How do I replace the "parent" property?

,

In GTK3 I am using allmost always the “child”/"parent" properties more over when I need to avoid UI files (pelase do not ask why), like in the case of GtkShortcutsWindow:

shortcut

#include <gtk/gtk.h>

static GtkShortcutsWindow *create_shortcuts_window ( void )
{
    /// ***
    GtkShortcutsWindow  *shortcuts_window = g_object_new ( GTK_TYPE_SHORTCUTS_WINDOW,
                                            "modal",         TRUE,
                                            "section-name",  "My_Section",
                                            "view-name",     "view",
                                            NULL );

    /// ***
    GtkShortcutsSection *shrt_section = g_object_new ( GTK_TYPE_SHORTCUTS_SECTION,
                                        "max-height",   10,
                                        "visible",      TRUE,
                                        "section-name", "My_Section",
                                        "title",        "What ever",
                                        "parent",       shortcuts_window,
                                        NULL );

    /// ***
    GtkShortcutsGroup *shrt_group = g_object_new ( GTK_TYPE_SHORTCUTS_GROUP,
                                    "visible",  1,
                                    "title",    "The Accelerator",
                                    "view",     "My_View",
                                    "parent",   shrt_section,
                                    NULL );

    /// ***
    g_object_new ( GTK_TYPE_SHORTCUTS_SHORTCUT,
                   "visible",       TRUE,
                   "shortcut-type", GTK_SHORTCUT_ACCELERATOR,
                   "accelerator",   "<ctl><alt>G",
                   "title",         "Take a Look",
                   "subtitle",      "Did you?",
                   "subtitle-set",  TRUE,
                   "direction",     GTK_TEXT_DIR_LTR,
                   "parent",        shrt_group,
                   NULL );

    /// ***
    return shortcuts_window;
}

static void activate_clbk ( GtkApplication *application )
{
    g_return_if_fail ( GTK_IS_APPLICATION ( application ) );

    GtkWidget           *window;
    GtkShortcutsWindow  *shortcuts_window;

    /// ***
    window = gtk_application_window_new ( application );

    /// ***
    shortcuts_window = create_shortcuts_window ();
    gtk_application_window_set_help_overlay ( GTK_APPLICATION_WINDOW ( window ), shortcuts_window );

    /// ***
    gtk_window_present ( GTK_WINDOW ( window ) );
    gtk_window_present ( GTK_WINDOW ( shortcuts_window ) );
}

int main ( void )
{
    GtkApplication *application;

    /// ***
    application = gtk_application_new ( "this.is.my.app", G_APPLICATION_FLAGS_NONE );

    /// ***
    g_signal_connect_swapped ( application, "activate", G_CALLBACK ( activate_clbk ), application );

    /// ***
    return g_application_run ( G_APPLICATION ( application ), 0, NULL );
}

The above code compiles fine in GTK4 as well but I get in terminal the following:

(GtkApplication:4936): GLib-GObject-CRITICAL **: 12:28:38.007: g_object_new_is_valid_property: property 'parent' of object class 'GtkShortcutsSection' is not writable

(GtkApplication:4936): GLib-GObject-CRITICAL **: 12:28:38.007: g_object_new_is_valid_property: property 'parent' of object class 'GtkShortcutsGroup' is not writable

(GtkApplication:4936): GLib-GObject-CRITICAL **: 12:28:38.008: g_object_new_is_valid_property: property 'parent' of object class 'GtkShortcutsShortcut' is not writable

How should I go in GTK4 with this one?

The way Object Hierarchy works:

GObject
    ╰── GInitiallyUnowned
        ╰── GtkWidget
            ╰── GtkBox
                ╰── GtkShortcutsSection

looks like the GtkShortcutsSection is derived from GtkWidget, but id does not inerhit the “parent” property or at least does not let me write it.
What I am missing here?

It does, but the property isn’t writable anymore. That’s because the generic gtk_container_add() method that was used to implement it is gone, and children have to be added to a parent with widget-specific API (like gtk_box_append() or gtk_window_set_child()).

1 Like

After I do the changes:

/// ***
gtk_window_set_child ( GTK_WINDOW ( shortcuts_window ), GTK_WIDGET ( shrt_section ) );

/// ***
gtk_box_append ( GTK_BOX ( shrt_section ), GTK_WIDGET ( shrt_group ) );

/// ***
gtk_box_append ( GTK_BOX ( shrt_group ), GTK_WIDGET ( shrt_shrt ) );

the program compiles fine, but gives Segmentation fault after I click on the x button of the GtkShortcutsWindow.

segfault

I think that I am not entirely prepared for GTK4 already.

This is what i get with GDB:

Starting program: /home/michi/Templates/GtkApplication/bin/Debug/GtkApplication 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff6247700 (LWP 2642)]
[New Thread 0x7ffff5a46700 (LWP 2643)]
[New Thread 0x7fffe3e78700 (LWP 2644)]
[New Thread 0x7fffe19ce700 (LWP 2645)]
[New Thread 0x7fffe1078700 (LWP 2646)]
[New Thread 0x7fffe0877700 (LWP 2647)]
[New Thread 0x7fffd705b700 (LWP 2648)]
[New Thread 0x7fffd685a700 (LWP 2649)]
[New Thread 0x7fffd5ed9700 (LWP 2650)]
[Thread 0x7fffd5ed9700 (LWP 2650) exited]

Thread 1 "GtkApplication" received signal SIGSEGV, Segmentation fault.
0x00007ffff799d254 in gtk_search_bar_set_search_mode (bar=0x555555700300, search_mode=search_mode@entry=0) at ../gtk/gtk/gtksearchbar.c:463
463	  g_return_if_fail (GTK_IS_SEARCH_BAR (bar

You’re calling a GtkSearchBar function with an invalid instance.

Could you please point me where exactly I am doing that in my code?
All I do is to run the program and click on the X button.

I haven’t seen your code, so how can I answer that question?

Use bt inside GDB to print out the whole backtrace, and you’ll get the location of all callers involved.

The code is in the first post.

With the actual changes:

#include <gtk/gtk.h>

static GtkShortcutsWindow *create_shortcuts_window ( void )
{
    /// ***
    GtkShortcutsWindow  *shortcuts_window = g_object_new ( GTK_TYPE_SHORTCUTS_WINDOW,
                                            "modal",         TRUE,
                                            "section-name",  "My_Section",
                                            "view-name",     "view",
                                            NULL );

    /// ***
    GtkShortcutsSection *shrt_section = g_object_new ( GTK_TYPE_SHORTCUTS_SECTION,
                                        "max-height",   10,
                                        "visible",      TRUE,
                                        "section-name", "My_Section",
                                        "title",        "What ever",
                                        NULL );

    /// ***
    GtkShortcutsGroup *shrt_group = g_object_new ( GTK_TYPE_SHORTCUTS_GROUP,
                                    "visible",  1,
                                    "title",    "The Accelerator",
                                    "view",     "My_View",
                                    NULL );

    /// ***
    GtkShortcutsShortcut *shrt_shrt = g_object_new ( GTK_TYPE_SHORTCUTS_SHORTCUT,
                                      "visible",       TRUE,
                                      "shortcut-type", GTK_SHORTCUT_ACCELERATOR,
                                      "accelerator",   "<ctl><alt>G",
                                      "title",         "Take a Look",
                                      "subtitle",      "Did you?",
                                      "subtitle-set",  TRUE,
                                      "direction",     GTK_TEXT_DIR_LTR,
                                      NULL );

    /// ***
    gtk_window_set_child ( GTK_WINDOW ( shortcuts_window ), GTK_WIDGET ( shrt_section ) );

    /// ***
    gtk_box_append ( GTK_BOX ( shrt_section ), GTK_WIDGET ( shrt_group ) );

    /// ***
    gtk_box_append ( GTK_BOX ( shrt_group ), GTK_WIDGET ( shrt_shrt ) );

    /// ***
    return shortcuts_window;
}

static void activate_clbk ( GtkApplication *application )
{
    g_return_if_fail ( GTK_IS_APPLICATION ( application ) );

    GtkWidget           *window;
    GtkShortcutsWindow  *shortcuts_window;

    /// ***
    window = gtk_application_window_new ( application );

    /// ***
    shortcuts_window = create_shortcuts_window ();
    gtk_application_window_set_help_overlay ( GTK_APPLICATION_WINDOW ( window ), shortcuts_window );

    /// ***
    gtk_window_present ( GTK_WINDOW ( window ) );
    gtk_window_present ( GTK_WINDOW ( shortcuts_window ) );
}

int main ( void )
{
    GtkApplication *application;

    /// ***
    application = gtk_application_new ( "this.is.my.app", G_APPLICATION_FLAGS_NONE );

    /// ***
    g_signal_connect_swapped ( application, "activate", G_CALLBACK ( activate_clbk ), application );

    /// ***
    return g_application_run ( G_APPLICATION ( application ), 0, NULL );
}

EDIT:
It happens after I call:

gtk_window_set_child ( GTK_WINDOW ( shortcuts_window ), GTK_WIDGET ( shrt_section ) );

The shortcuts window has a search bar that allows searching/filtering shortcuts. The backtrace will tell you where things are breaking.

To be fair, constructing the shortcuts window in code is really not how you’re supposed to use it; the idiomatic way is to use a GtkBuilder UI description file.

Yes, but this is the thing here, I need it like this.
So I cannot figure out why I get that Segfault and I would like to get a fix around this.

Once again, the backtrace will tell you what’s calling the GtkSearchBar function. Then you can look at the code inside GTK to see what are the expectations, and if you’re not following them.

The reason why I’m telling you to use an XML file is that it’s harder to break than just randomly adding widgets.

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