简体   繁体   中英

gtkmm builder from glade file doesn't work

My gtkmm program won't show any windows. Compile works fine. These are the messages I get when running:

(process:2312): GLib-GObject-CRITICAL **: /build/buildd/glib2.0-2.24.1/gobject    
/gtype.c:2706: You forgot to call g_type_init()
(process:2312): GLib-CRITICAL **: g_once_init_leave: assertion 
`initialization_value != 0' failed
(process:2312): GLib-GObject-CRITICAL **: g_object_newv: assertion 
`G_TYPE_IS_OBJECT (object_type)' failed

Code is:

int main(int argc, char** argv) {
    Glib::RefPtr<Gtk::Builder> builder =   
         Gtk::Builder::create_from_file("basic.glade");
    Gtk::Window* pMyWindow = 0;
    builder->get_widget("window1", pMyWindow);
    pMyWindow->show();
}

It doesn't matter if basic.glade is a valid file or does not exist. I tried with the whole path file instead of basic.glade.
When I debug it, I realize that the program never gets out of that builder first line. It's like it keeps running, so when I pause the only process, either will be at g_once_init_enter_impl() or at g_slist_find() (being called by the former). So I don't ever see any window (because it never gets to that pMyWindow->show() line)

Add the following line as your first line in main :

Gtk::Main kit(argc, argv);

According to the Gtkmm Hello World tutorial :

First we instantiate an object called kit. This is of type Gtk::Main. Every gtkmm program must have one of these.

This object is the responsible to initialize all the GTK+ library, that includes a call to the g_type_init function your error talk about.

Of course, you can use any name you like instead of kit .

UPDATE: Oh, and you should call to:

Gtk::Main::run();

Or equivalently:

kit.run();

as kindly noted by Erandros in the comment. Without it the program will finish immediatley. It will issue a gtk_main() call that will run the main loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM