如何在gtk4中使用eventcontroller

huangapple go评论78阅读模式
英文:

How to use eventcontroller in gtk4

问题

我想要为 GtkBox 小部件添加 onclick 事件处理。我看到只有 GtkButtonclicked 信号,所以我觉得唯一的选项是将 GtkEventController 附加到我的小部件上。

根据文档,gtk_get_current_event(GtkEventController *ctr) 在有事件时返回事件,否则返回 NULL。我该如何监听这个函数?我应该在主应用程序循环中实现手动的 while 循环吗?感觉应该有最佳实践方式。

提前谢谢。

英文:

I would like to add onclick event handling to a GtkBox widget. I see that only GtkButton has clicked signal, so I feel like the only option is to attach a GtkEventController to my widget.

As per the doc, gtk_get_current_event(GtkEventController *ctr) returns the events if there are any and NULL otherwise. How do I listen to this function? Should I implement manual while loop for the main application loop? It feels like there must be a best practice for this.

Thank you in advance

答案1

得分: 2

要确定在GtkBox上的事件,您可以创建一个新的事件控制器来监听事件并为您关心的事件添加信号回调。

对于您的情况,您可以使用gtk_gesture_click_new()创建一个新的手势事件控制器,并为released信号添加回调函数。

以下是一个最小示例:

/* button-event.c
 *
 * 编译:cc -ggdb button-event.c -o button-event $(pkg-config --cflags --libs gtk4)
 * 运行:./button-event
 *
 * 作者:Mohammed Sadiq <www.sadiqpk.org>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later OR CC0-1.0
 */

#include <gtk/gtk.h>

static gboolean
gesture_released_cb (GtkWidget *widget)
{
  g_warning ("执行某些操作");

  return GDK_EVENT_STOP;
}

static void
app_activated_cb (GtkApplication *app)
{
  GtkGesture *gesture;
  GtkWindow *window;
  GtkWidget *box, *label;

  window = GTK_WINDOW (gtk_application_window_new (app));
  gtk_window_set_default_size (window, 400, 300);

  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
  label = gtk_label_new ("Hello");
  gtk_box_append (GTK_BOX (box), label);
  gtk_window_set_child (window, box);

  gesture = gtk_gesture_click_new ();

  g_signal_connect_object (gesture, "released",
                           G_CALLBACK (gesture_released_cb),
                           box, G_CONNECT_SWAPPED);
  gtk_widget_add_controller (label, GTK_EVENT_CONTROLLER (gesture));

  gtk_window_present (window);
}

int
main (int   argc,
      char *argv[])
{
  g_autoptr(GtkApplication) app = gtk_application_new (NULL, 0);

  g_signal_connect (app, "activate", G_CALLBACK (app_activated_cb), NULL);

  return g_application_run (G_APPLICATION (app), argc, argv);
}
英文:

To determine events over a GtkBox, you can create a new event controller to listen to events and add a signal callback for the events you care.

For your case, you can create a new gesture event controller with gtk_gesture_click_new () and add a callback to the signal released.

Here is a minimal example:

/* button-event.c
 *
 * Compile: cc -ggdb button-event.c -o button-event $(pkg-config --cflags --libs gtk4)
 * Run: ./button-event
 *
 * Author: Mohammed Sadiq <www.sadiqpk.org>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later OR CC0-1.0
 */

#include <gtk/gtk.h>

static gboolean
gesture_released_cb (GtkWidget *widget)
{
  g_warning ("do something");

  return GDK_EVENT_STOP;
}

static void
app_activated_cb (GtkApplication *app)
{
  GtkGesture *gesture;
  GtkWindow *window;
  GtkWidget *box, *label;

  window = GTK_WINDOW (gtk_application_window_new (app));
  gtk_window_set_default_size (window, 400, 300);

  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
  label = gtk_label_new ("Hello");
  gtk_box_append (GTK_BOX (box), label);
  gtk_window_set_child (window, box);

  gesture = gtk_gesture_click_new ();

  g_signal_connect_object (gesture, "released",
                           G_CALLBACK (gesture_released_cb),
                           box, G_CONNECT_SWAPPED);
  gtk_widget_add_controller (label, GTK_EVENT_CONTROLLER (gesture));

  gtk_window_present (window);
}

int
main (int   argc,
      char *argv[])
{
  g_autoptr(GtkApplication) app = gtk_application_new (NULL, 0);

  g_signal_connect (app, "activate", G_CALLBACK (app_activated_cb), NULL);

  return g_application_run (G_APPLICATION (app), argc, argv);
}

huangapple
  • 本文由 发表于 2023年6月30日 02:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定