Re: [gtkmm] GTKMM classes handling local variables



Benoît Touron wrote:

Ian,

Your _label gets destroyed when you leave the constructor
(MyClass::MyClass), because it's local.
That's probably why it is not desplayed.

You could either declare it in the class, or allocate it with new and use Gtk::Manage on the pointer:

This code should work:

MyClass::MyClass()
{
    Label *_label = Gtk::Manage( new Label("Hello World"));
    m_box.pack_start(*_label, PACK_SHRINK);
    show_all_children();
}


Ben

Hi all,
Maybe someone can help me with this, I have only been developing with
GTKMM for a little while so I am a little bit of a newbie.

Right here is a piece of example code:

class MyClass : public Gtk::Window
{
    public:
        MyClass();
        virtual ~MyClass();
    private:
        VBox m_box;
};

MyClass::MyClass()
{
    Label _label("Hello World");
    m_box.pack_start(_label, PACK_SHRINK);
    show_all_children);
}

MyClass::~MyClass()
{
}

Right what I cannot understand is why this program will compile but my
label will not be displayed? For some reason it will only work if I
declare my Label in the class definition... I don't see why it is not
working... :(

Ian

_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list




_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list


Thanks for the advice, I think I will give GTK::Manage a try, I think it will take a little getting used to, usually 90% of my c++ code is based around pointers, even my old GTK stuff (I used to wrap GTK into C++ before I discovered GTKMM). I would have thought the GTKMM team would have catered for the use of local varibles in some way, hence my obvious mistake of just trying to use them, I do however love the way GTKMM does a lot of things better than QT, oh well, right thanks again for the advice, I should now be able to continue my projects (Gnome Easy CD -- website and source tree coming soonish, Groupmage - native linux client for Groupwise -- even though I use sun JDS, Fedora and Debian lol, -- GnomeEasyInstall and GnomeAutoCD) hmm :-D




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]