Cannot access button defined in the UI file

I have the following UI file:

<interface>
  <template class="OTPClientWindow" parent="AdwApplicationWindow">
    <property name="default-width">400</property>
    <property name="default-height">600</property>
    <property name="content">
      <object class="AdwToolbarView">
        <property name="top-bar-style">ADW_TOOLBAR_RAISED</property>
        <child type="top">
          <object class="AdwHeaderBar">
            <child type="title">
              <object class="AdwWindowTitle" id="title">
                <property name="title">OTPClient</property>
              </object>
            </child>
            <child type="end">
              <object class="GtkToggleButton" id="search_button">
                <property name="icon-name">system-search-symbolic</property>
              </object>
            </child>
          </object>
        </child>
        <child>
          <object class="GtkBox">
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkSearchBar" id="search_bar">
                <property name="search-mode-enabled" bind-source="search_button" bind-property="active" bind-flags="bidirectional"/>
                <signal name="notify::search-mode-enabled" handler="search_enabled" swapped="no" />
                <child>
                  <object class="GtkSearchEntry" id="search_entry">
                    <property name="placeholder-text" translatable="yes">Find token</property>
                    <signal name="search-changed" handler="search_changed" swapped="no" />
                    <signal name="stop-search" handler="stop_search" swapped="no" />
                  </object>
                </child>
              </object>
            </child>
          </object>
        </child>
      </object>
    </property>
  </template>
</interface>

and the following C code in my otpclient-window.c file:

struct _OTPClientWindow
{
    AdwApplicationWindow parent;
    GtkToggleButton *search_button;
    GtkSearchBar *search_bar;
    GtkSearchEntry *search_entry;
};

G_DEFINE_FINAL_TYPE (OTPClientWindow, otpclient_window, ADW_TYPE_APPLICATION_WINDOW)

static void
otpclient_window_dispose (GObject *object)
{
    gtk_widget_dispose_template (GTK_WIDGET (object), OTPCLIENT_TYPE_WINDOW);
    G_OBJECT_CLASS (otpclient_window_parent_class)->dispose (object);
}

static void
search_func (OTPClientWindow *self)
{
    gtk_toggle_button_set_active (self->search_button, TRUE);
}

static void
otpclient_window_init (OTPClientWindow *self)
{
    gtk_widget_init_template (GTK_WIDGET(self));
}

static void
otpclient_window_class_init (OTPClientWindowClass *klass)
{
    G_OBJECT_CLASS(klass)->dispose = otpclient_window_dispose;

    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
    gtk_widget_class_set_template_from_resource (widget_class, "/com/github/paolostivanin/OTPClient/ui/window.ui");
    gtk_widget_class_bind_template_child (widget_class, OTPClientWindow, search_button);
    gtk_widget_class_bind_template_child (widget_class, OTPClientWindow, search_bar);
    gtk_widget_class_bind_template_child (widget_class, OTPClientWindow, search_entry);

    gtk_widget_class_add_binding_action (widget_class, GDK_KEY_q, GDK_CONTROL_MASK, "window.close", NULL);

    gtk_widget_class_install_action (widget_class, "window.search", NULL, (GtkWidgetActionActivateFunc)search_func);
    gtk_widget_class_add_binding_action (widget_class, GDK_KEY_f, GDK_CONTROL_MASK, "window.search", NULL);
}

GtkWidget *
otpclient_window_new (OTPClientApplication *application)
{
    return g_object_new (OTPCLIENT_TYPE_WINDOW, "application", application, NULL);
}

When Iaunch the application and click on the search icon, the search bar appears and all it’s good. But when I type “ctrl-f” I get:

gtk_toggle_button_set_active: assertion 'GTK_IS_TOGGLE_BUTTON (toggle_button)' failed

why’s that? What am I missing?

Can you share a complete self-contained example that fails? I can’t reproduce that error here.

like always, it was a user error :slight_smile: I was missing some params in the search_func:

before:

static void
search_func (OTPClientWindow *self)
{
...
}

after:

static void
search_func (OTPClientWindow *self,
             const gchar     *action_name,
             GVariant        *parameter)
{
...
}

all good now!

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