GTK状态栏如何工作?我的代码出了什么问题?

时间:2009-12-23 14:56:45

标签: c gtk

我想有一个状态栏,所以我开始制作一个只有状态栏的小程序,所以我可以看到它是如何工作的。

现在我只想在其中获取一些文字,但它会显示一个随机字符。

有人可以看到我的代码出了什么问题吗?

#include <gtk/gtk.h>

int main (int argc, char *argv[]) {
  GtkWidget *window, *statusbar, *vbox;
  gchar *info;
  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_size_request(window, 250, -1);

  g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

  statusbar = gtk_statusbar_new();

  /* stack for info messages */
  g_object_set_data(G_OBJECT(statusbar), "info", (gpointer) "1");
  g_object_set_data(G_OBJECT(statusbar), "info", (gpointer) "2");
  g_object_set_data(G_OBJECT(statusbar), "info", (gpointer) "3");

  /* stack for warning messages */
  g_object_set_data(G_OBJECT(statusbar), "warning", (gpointer) "A");
  g_object_set_data(G_OBJECT(statusbar), "warning", (gpointer) "B");
  g_object_set_data(G_OBJECT(statusbar), "warning", (gpointer) "C");

  /* get id for the message at the top of the info stack? */
  guint id = gtk_statusbar_get_context_id(statusbar, "info");
  /* show the top message from the info stack ? */
  gtk_statusbar_push(statusbar, id, info);

  vbox = gtk_vbox_new(FALSE, 5);  
  gtk_box_pack_start_defaults(GTK_BOX (vbox), statusbar);
  gtk_container_add(GTK_CONTAINER(window), vbox);

  gtk_widget_show_all(window);
  gtk_main();
  return 0;
}

我收到这些警告

s.c:26: warning: passing argument 1 of ‘gtk_statusbar_get_context_id’ from incompatible pointer type
/usr/include/gtk-2.0/gtk/gtkstatusbar.h:94: note: expected ‘struct GtkStatusbar *’ but argument is of type ‘struct GtkWidget *’
s.c:28: warning: passing argument 1 of ‘gtk_statusbar_pop’ from incompatible pointer type
/usr/include/gtk-2.0/gtk/gtkstatusbar.h:100: note: expected ‘struct GtkStatusbar *’ but argument is of type ‘struct GtkWidget *’
s.c:28: error: too many arguments to function ‘gtk_statusbar_pop’

1 个答案:

答案 0 :(得分:4)

您声明了GtkWidget *statusbar,但gtk_statusbar_...方法期望GtkStatusbar *:使用GTK_STATUSBAR(statusbar)投射它会删除警告。这不是一个大问题,因为该程序无论如何都会起作用:最大的问题是你在没有初始化的情况下使用info

以下代码在此处按预期工作:

/* gcc -o status status.c $(pkg-config --cflags --libs gtk+-2.0) */

#include <gtk/gtk.h>

int main (int argc, char *argv[]) {
    GtkWidget *window, *statusbar, *vbox;
    gchar *info;
    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request(window, 250, -1);

    g_signal_connect(G_OBJECT(window), "destroy",
                     G_CALLBACK(gtk_main_quit), NULL);

    statusbar = gtk_statusbar_new();

    /* stack for info messages */
    g_object_set_data(G_OBJECT(statusbar), "info", (gpointer)
                      "1");
    g_object_set_data(G_OBJECT(statusbar), "info",
                      (gpointer) "2");
    g_object_set_data(G_OBJECT(statusbar), "info",
                      (gpointer) "3");

    /* stack for warning messages */
    g_object_set_data(G_OBJECT(statusbar), "warning",
                      (gpointer) "A");
    g_object_set_data(G_OBJECT(statusbar), "warning",
                      (gpointer) "B");
    g_object_set_data(G_OBJECT(statusbar),
                      "warning", (gpointer) "C");

    /* get id for the message at the top of the
     * info stack? */
    guint id = gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), "info");
    /* show the top message from the info stack
     * ? */
    info = "This was uninitialized";
    gtk_statusbar_push(GTK_STATUSBAR(statusbar), id, info);

    vbox = gtk_vbox_new(FALSE, 5);
    gtk_box_pack_start_defaults(GTK_BOX
                                (vbox),
                                statusbar);
    gtk_container_add(GTK_CONTAINER(window),
                      vbox);

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

我不知道你想用g_object_set_data试图实现什么,但也许它们只是旧测试......

<强>附录:

您应该使用gtk_statusbar_push()在状态栏堆栈上堆积消息。使用gtk_statusbar_pop(),您将删除最后推送的邮件,发现之前的邮件。

gtk_statusbar_push(GTK_STATUSBAR(statusbar), id, "First message");
// Now the statusbar shows "First message"
gtk_statusbar_push(GTK_STATUSBAR(statusbar), id, "Second message");
// Now the statusbar shows "Second message"
gtk_statusbar_pop(GTK_STATUSBAR(statusbar), id);
// Now the statusbar shows "First message"