Subclassing GtkApplicationWindow + calling gtk_window_get_application

,

Hi,

I’m trying to subclass GtkApplicationWindow and need to call gtk_window_get_application() to access a property of my custom application class (a subclass of GtkApplication) needed by one of the widgets that the window will contain. I’ve tried calling gtk_window_get_application() inside the sub-class’s overridden ‘constructed’ method, where I’m creating the widgets, and it just returns NULL despite ‘application’ being passed during window construction:

MbApplicationWindow* mb_application_window_new(MbApplication* application) {
    g_assert(MB_IS_APPLICATION(application));

    return g_object_new(MB_TYPE_APPLICATION_WINDOW,
                        "application", GTK_APPLICATION(application),
                        NULL);
}

My understanding is that an object’s ‘constructed’ method is invoked after all construction properties have been set, so why would gtk_window_get_application() return NULL when called from that method?

When you override ::constructed, did you chain up to the parent ::constructed method first?

Something like this at the top of the function:

	if (G_OBJECT_CLASS (mb_application_window_parent_class)->constructed != NULL)
	{
		G_OBJECT_CLASS (mb_application_window_parent_class)->constructed (object);
	}

I’d neglected the ‘if’ test, but yes:

static void mb_application_window_constructed(GObject* object) {
    MbApplicationWindow* window;
    MbApplication* application;
    GtkExpression* expression;

    G_OBJECT_CLASS(mb_application_window_parent_class)->constructed(object);

    window = MB_APPLICATION_WINDOW(object);
    application = MB_APPLICATION(gtk_window_get_application(GTK_WINDOW(window)));

    g_assert(MB_IS_APPLICATION(application)); // <== FAILS

    gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);

    window->header_bar = GTK_HEADER_BAR(gtk_header_bar_new());
    gtk_header_bar_set_title_widget(window->header_bar, gtk_label_new("Midibug"));
    gtk_header_bar_set_show_title_buttons(window->header_bar, TRUE);
    gtk_window_set_titlebar(GTK_WINDOW(window), GTK_WIDGET(window->header_bar));

    expression = gtk_property_expression_new(
            MB_TYPE_MIDI_PORT_DESCRIPTOR,
            NULL,
            "port-name");

    window->port_select_drop_down = GTK_DROP_DOWN(gtk_drop_down_new(
            G_LIST_MODEL(mb_application_get_midi_port_list(application)),
            expression
    ));

    gtk_header_bar_pack_start(window->header_bar, GTK_WIDGET(window->port_select_drop_down));
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.