GTK +,C ++,Windows,绑定到林间空地无法正常工作

时间:2018-12-14 18:46:54

标签: gtk

Windows 10

GTK_GladeInWindowsSample.c文件是通过命令编译的

$ gcc GTK_GladeInWindowsSample.c -o GTK_GladeInWindowsSample -mwindows`pkg-config --cflags --libs gtk + -3.0 gmodule-2.0 The line of command in the form of a picture, because some characters are not displayed

#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;
}

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>

当我按下按钮时,什么也没发生(应该将文本更改为“单击”)。也不叫destroy _

1 个答案:

答案 0 :(得分:1)

查看gtk_builder_connect_signals()上的文档:

  

在为Windows编译应用程序时,必须声明信号   使用G_MODULE_EXPORT的回调,否则将不将它们放在符号中   表。在Linux和Unices上,这不是必需的。申请应   而是使用-Wl,-export-dynamic CFLAGS进行编译并链接   针对gmodule-export-2.0。

因此您的处理程序声明必须类似于

G_MODULE_EXPORT void clicked(GtkButton *button, gpointer user_data)

或者在每个回调函数上调用gtk_builder_add_callback_symbol()