Can't Make GtkApplication run a Glib::Ref<Gtk::ApplicationWindow> given by GTK::Builder

107 views Asked by At

I'm trying to run an simple Gtkmm program but I'm failing to get the content to the GlibRef. Here's a sample code:

#include <gtkmm-3.0/gtkmm.h>

int main(int argc, char** argv)
{
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create (argc, argv, "org.me.myprogram");
    Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("gladefile.glade");
    Glib::RefPtr<Gtk::ApplicationWindow> myMainWindow= builder->get_object("myGTkApplicationWindowID");
    app->run(myMainWindow, argc, argv);
    return 0;
}

This gives me the following error(showing the important part):

error: no matching function for call to ‘Gtk::Application::run(Glib::RefPtr<Gtk::ApplicationWindow>&, int&, char**&)
note:   no known conversion for argument 1 from ‘Glib::RefPtr<Gtk::ApplicationWindow>’ to ‘Gtk::Window&

Trying to use *myMainWindow doesn't work because this operator is not defined, the operator-> is defined but doesn't work because it goes deeper than needed, showing the following error:

error: no matching function for call to ‘Gtk::Application::run(Gtk::ApplicationWindow*, int&, char**&)’
note:   no known conversion for argument 1 from ‘Gtk::ApplicationWindow*’ to ‘Gtk::Window&’

Even tried more wild things like &(myMainWindow.operator()) and *(myMainWindow.operator()), but I believe I'm missing the ideia of this smart pointer. So how to make it work? AFAIK GTKMM should work very easily with Glib::RefPtr.

1

There are 1 answers

0
NoDakker On

I tried out your code also testing various referencing and de-referencing of the "myMainWindow" object. That led me to review the "gtkmm 3" documentation as it pertains to using widgets from a glade builder file. In their examples they seemed to reference widgets instead of objects. With that in mind, I revised your code as such.

#include <gtkmm-3.0/gtkmm.h>

int main(int argc, char** argv)
{
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.me.myprogram");
    Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("gladefile.glade");
    Gtk::ApplicationWindow* myMainWindow = nullptr;
    builder->get_widget("myGTkApplicationWindowID", myMainWindow);
    app->run(*myMainWindow, argc, argv);
    return 0;
}

Defining the application window as a GTK widget and then using the "builder->get_widget" function seemed to retrieve the appropriate widget/object that could be referenced in the "app->run" call. The program did then compile and produced a window.

Sample Window for GTKMM3

I am not sure how much you are committed to object definitions versus widget definitions, but you might want to reference the following link I used to make the above revisions.

"http://transit.iut2.upmf-grenoble.fr/doc/gtkmm-3.0/tutorial/html/chapter-builder.html"

I hope that helps.

Regards.