g_timeout_add ()

本文介绍如何使用GTK的g_timeout_add函数创建一个简单的实时显示窗口时钟。通过定时更新并绘制时间,演示了基本的GTK窗口绘制及事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在GTK中,如果您要定时让程序去作某件事,则可以使用g_timeout_add()或g_timeout_add_full().一个例子如下:

 

这个例子的作用就是把当前时间显示到窗口中,即显示了一个实时时钟。
//~~~~~~~ begin of program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <cairo.h>
#include <gtk/gtk.h>
#include <time.h>


static char buffer[256];
/******************************
*  把buffer显示到窗口中
*  每次画窗口时调用
*/
static gboolean
on_expose_event(GtkWidget *widget,
    GdkEventExpose *event,
    gpointer data)
{
  cairo_t *cr;

  cr = gdk_cairo_create(widget->window);

  cairo_move_to(cr, 30, 30);
  cairo_show_text(cr, buffer);

  cairo_destroy(cr);

  return FALSE;
}
 
/******************************
*  把当前时间打印到buffer中,并且重画窗口
*  每次timeout后调用,即每秒调用一次
*/
static gboolean
time_handler(GtkWidget *widget)
{
  if (widget->window == NULL) return FALSE;

  time_t curtime;
  struct tm *loctime;

  curtime = time(NULL);
  loctime = localtime(&curtime);
  strftime(buffer, 256, "%T", loctime);

  gtk_widget_queue_draw(widget);
  return TRUE;
}
int
main (int argc, char *argv[])
{

  GtkWidget *window;
  GtkWidget *darea;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  darea = gtk_drawing_area_new();
  gtk_container_add(GTK_CONTAINER (window), darea);

  g_signal_connect(darea, "expose-event",
      G_CALLBACK(on_expose_event), NULL);   // 每次画窗口时的callback
  g_signal_connect(window, "destroy",
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 170, 100);

  gtk_window_set_title(GTK_WINDOW(window), "timer");
  g_timeout_add(1000, (GSourceFunc) time_handler, (gpointer) window);
  gtk_widget_show_all(window);
  time_handler(window);

  gtk_main();

  return 0;
}
//~~~~~~~ end of program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

例子来源:

http://zetcode.com/tutorials/gtktutorial/gtkevents/

 

 

这两个函数的说明见下:

g_timeout_add ()

guint               g_timeout_add                       (guint interval,
GSourceFunc function,
gpointer data);

Sets a function to be called at regular intervals, with the default priority, G_PRIORITY_DEFAULT. The function is called repeatedly until it returns FALSE, at which point the timeout is automatically destroyed and the function will not be called again. The first call to the function will be at the end of the first interval.

Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given interval (it does not try to 'catch up' time lost in delays).

 

interval :the time between calls to the function, in milliseconds (1/1000ths of a second)
function :function to call
data :data to pass to function
Returns :the ID (greater than 0) of the event source.

第一个参数是间隔的毫秒数,第二个参数是定时后的callback,第三个是传递给callback的数据。callback的形式如下:

          gint timeout_callback(gpointer data);

g_timeout_add的返回值可以用来结束这个timeout,如下(假如返回放到tag中)

          void g_source_remove(gint tag);

也可以让callback返回0或FALSE来结束timeout。

更多的参考可见GTK+tutorial 相关章节:

http://library.gnome.org/devel/gtk-tutorial/stable/c1761.html#SEC-TIMEOUTS

 

 

g_timeout_add_full ()

guint               g_timeout_add_full                  (gint priority,
guint interval,
GSourceFunc function,
gpointer data,
GDestroyNotify notify);

Sets a function to be called at regular intervals, with the given priority. The function is called repeatedly until it returns FALSE, at which point the timeout is automatically destroyed and the function will not be called again. The notify function is called when the timeout is destroyed. The first call to the function will be at the end of the first interval.

Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given interval (it does not try to 'catch up' time lost in delays).

 

priority :the priority of the idle source. Typically this will be in the range between G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE.
interval :the time between calls to the function, in milliseconds (1/1000ths of a second)
function :function to call
data :data to pass to function
notify :function to call when the idle is removed, or NULL
Returns :the ID (greater than 0) of the event source.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值