Владимир В
Владимир В

Reputation: 57

GTK+, C++, Windows, Binding to glade not working

Windows 10

File GTK_GladeInWindowsSample.c is compiled by the command

$ gcc GTK_GladeInWindowsSample.c -o GTK_GladeInWindowsSample -mwindows `pkg-config --cflags --libs gtk+-3.0 gmodule-2.0`

File GTK_GladeInWindowsSample.c

#include <gtk/gtk.h>

GtkWidget *win=NULL; 
GtkBuilder *builder=NULL;
GError *error=NULL;
GtkButton *button=NULL;

void clicked(GtkButton *button, gpointer user_data)
{
    gtk_button_set_label(button, "clicked");
}

void destroy_(GtkWidget *object, gpointer user_data)
{
    gtk_main_quit();
}

int main (int argc, char *argv[])
{
    gtk_init(&argc, &argv);
    builder=gtk_builder_new();

    if(!gtk_builder_add_from_file(builder, "GTK_GladeInWindowsSample.glade", &error))
    {
        return 0;
    }

    win=GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
    button=GTK_BUTTON(gtk_builder_get_object(builder, "button1"));
    gtk_widget_realize(win);
    gtk_builder_connect_signals(builder, NULL);  
    gtk_widget_show_all(win);
    g_object_unref(builder);

    gtk_main();

    return 0;
}

File GTK_GladeInWindowsSample.glade

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <signal name="destroy" handler="destroy_" swapped="no"/>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkButton" id="button1">
        <property name="label" translatable="yes">button</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <property name="margin_left">50</property>
        <property name="margin_right">50</property>
        <property name="margin_top">50</property>
        <property name="margin_bottom">50</property>
        <signal name="clicked" handler="clicked" swapped="no"/>
      </object>
    </child>
  </object>
</interface>

When I press button, nothing happens (it should change the text to 'clicked'). destroy_ is not called either

Upvotes: 2

Views: 576

Answers (1)

Jussi Kukkonen
Jussi Kukkonen

Reputation: 14587

Check the documentation on gtk_builder_connect_signals():

When compiling applications for Windows, you must declare signal callbacks with G_MODULE_EXPORT, or they will not be put in the symbol table. On Linux and Unices, this is not necessary; applications should instead be compiled with the -Wl,--export-dynamic CFLAGS, and linked against gmodule-export-2.0.

So your handler declarations need to look like

G_MODULE_EXPORT void clicked(GtkButton *button, gpointer user_data)

Alternatively call gtk_builder_add_callback_symbol() on every callback function.

Upvotes: 2

Related Questions