GTK 使用 glade 4 正确书写的方法

#include <gtk/gtk.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <stdlib.h>


GtkBuilder *builder;
GObject *window;
GObject *button;
GObject *button2;

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
  printf("fuckyou\n");
  char *buffer = "OK";
  gtk_button_set_label(GTK_BUTTON(button),buffer);
}

static void
print_hello2 (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
  gtk_button_set_label(GTK_BUTTON(widget),"fuckyou");
}


static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  /* Construct a GtkBuilder instance and load our UI description */
  builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "builder.ui", NULL);

  /* Connect signal handlers to the constructed widgets. */
  window = gtk_builder_get_object (builder, "window");
  gtk_window_set_application (GTK_WINDOW (window), app);

  button = gtk_builder_get_object (builder, "button1");
  button2 = gtk_builder_get_object (builder, "button2");


  g_signal_connect (button, "clicked", G_CALLBACK (print_hello2), button2);
  g_signal_connect (button2, "clicked", G_CALLBACK (print_hello), NULL);

  gtk_widget_set_visible (GTK_WIDGET (window), TRUE);

  /* We do not need the builder any more */
  g_object_unref (builder);
}

int
main (int   argc,
      char *argv[])
{
#ifdef GTK_SRCDIR
  g_chdir (GTK_SRCDIR);
#endif

  GtkApplication *app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);

  int status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

builder.ui

<?xml version='1.0' encoding='UTF-8'?>
<!-- Created with Cambalache 0.17.0 -->
<interface>
  <!-- interface-name builer.ui -->
  <requires lib="gtk" version="4.12"/>
  <object class="GtkWindow" id="window">
    <child>
      <object class="GtkFixed" id="fixed">
        <child>
          <object class="GtkButton" id="button1">
            <property name="label">button1</property>
            <property name="margin-start">24</property>
            <property name="margin-top">20</property>
          </object>
        </child>
        <child>
          <object class="GtkButton" id="button2">
            <property name="label">button2</property>
            <property name="margin-start">120</property>
            <property name="margin-top">20</property>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值