Andy Ef
Andy Ef

Reputation: 175

gtkmm: Cannot create Gtk::Switch

Using Gtkmm 2.24.5, I'm trying to create a window with a single Gtk::Switch with the following code:

#include <gtkmm.h>
#include <gtkmm/switch.h>

class SimpleWindow : public Gtk::Window
{
public:
    SimpleWindow();
private:
    Gtk::VBox m_VBox;
    Gtk::Switch m_Switch;
};

SimpleWindow::SimpleWindow()
{
    set_title("Simple");
    add(m_VBox);
    // Todo: Setup switch
    show_all();
}

int main(int argc, char** argv)
{
    Gtk::Main kit(argc, argv);
    SimpleWindow simple;
    kit.run(simple);
    return 0;
}

When trying to run the code, I get the following warnings and errors:

GLib-GObject-WARNING **: 11:21:22.896: cannot register existing type 'GtkWidget'
GLib-GObject-WARNING **: 11:21:22.896: cannot add class private field to invalid type '<invalid>' 
GLib-GObject-WARNING **: 11:21:22.896: cannot add private field to invalid (non-instantiatable) type '<invalid>' 
GLib-GObject-CRITICAL **: 11:21:22.896: g_type_add_interface_static: assertion 'G_TYPE_IS_INSTANTIATABLE (instance_type)' failed 
GLib-GObject-WARNING **: 11:21:22.896: cannot register existing type 'GtkBuildable' 
GLib-GObject-CRITICAL **: 11:21:22.896: g_type_interface_add_prerequisite: assertion 'G_TYPE_IS_INTERFACE (interface_type)' failed 
GLib-CRITICAL **: 11:21:22.896: g_once_init_leave: assertion 'result != 0' failed

After removing the switch from the class definition, the program runs just fine, returning an empty window. What is the problem here?

Upvotes: 1

Views: 198

Answers (1)

Andy Ef
Andy Ef

Reputation: 175

The problem was, that I used gtkmm 2.24.5, while Gtk::Switch was introduced with gtkmm 3.0. Somehow my pkg-config eclipse plugin also included gtkmm 4.0, so the explicit include of gtkmm/switch.h prevented a compiler error at the m_Switch declaration.

Switching to a clean project with gtkmm 4.0 the new minimum working example for the Gtk:Switch class looks like this:

#include <gtkmm.h>

class SimpleWindow : public Gtk::Window
{
public:
    SimpleWindow();
private:
    Gtk::Switch m_Switch;
};

SimpleWindow::SimpleWindow()
{
    set_title("Simple");
    m_Switch.set_margin(20);
    set_child(m_Switch);
}

int main(int argc, char** argv)
{
    auto app = Gtk::Application::create("org.gtkmm.example");
    return app->make_window_and_run<SimpleWindow>(argc, argv);
}

Which compiles and runs now as expected.

Upvotes: 1

Related Questions