October 24, 2024
Chicago 12, Melborne City, USA
C++

Accelerator in gtk ui builder not triggered


This is my ui glade file:

<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
  <requires lib="gtk" version="4.0"/>
  <menu id="menu1">
    <item>
      <attribute name="label" translatable="yes">A_dd</attribute>
      <attribute name="action">win.add</attribute>
    </item>
    <section>
      <item>
        <attribute name="label" translatable="yes">_About...</attribute>
        <attribute name="action">win.about</attribute>
        <attribute name="accel">&lt;Primary&gt;a</attribute>
      </item>
    </section>
    <section>
      <item>
        <attribute name="label" translatable="yes">_Close</attribute>
        <attribute name="action">app.close</attribute>
      </item>
    </section>
  </menu>
  <object class="GtkApplicationWindow" id="toplevel">
    <child>
      <object class="GtkGrid" id="grid">
        <child>
          <object class="GtkMenuButton">
            <property name="icon-name">menu</property>
            <property name="always-show-arrow">true</property>
            <property name="menu-model">menu1</property>
            <property name="has-frame">true</property>
            <layout>
              <property name="column">0</property>
              <property name="row">0</property>
            </layout>
          </object>
        </child>
      </object>
    </child>
    <property name="child">grid</property>
  </object>
</interface>

In on_activate function:

void on_activate(GtkApplication *app)
{
  const GActionEntry entries[] = {
    {"about", on_about, NULL, NULL, NULL, {0,0,0}}
  };
  
  GtkBuilder* builder = gtk_builder_new_from_resource(GLADE_FILE);
  GtkWindow *toplevel = GTK_WINDOW(gtk_builder_get_object(builder, "toplevel"));
  GtkIconTheme *icon_theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
  
  g_action_map_add_action_entries(G_ACTION_MAP(toplevel), entries, G_N_ELEMENTS (entries), app);
  gtk_window_set_application (toplevel, app);
  gtk_window_set_title (toplevel, PROGRAM_TITLE);
  gtk_icon_theme_add_resource_path (icon_theme, PROGRAM_RC_PATH);
  gtk_window_set_default_icon_name (PROGRAM_NAME);
    
  gtk_window_present(toplevel);
  g_object_unref(builder);
}

While in my application I can see the label accelerator "Ctrl+A", is not triggered with the keyboard, any ideas?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video