以编程方式将数据添加到Glade 3.12.1中创建的GtkTreeView / GtkListStore

时间:2013-06-28 17:11:59

标签: c gtk gtk3 glade gtktreeview

我一直在教自己如何使用C语言进行编码并利用GTK在Linux中编写应用程序,使用Glade在实现之前设计UI。我一直在尝试使用GtkTreeView和GtkListStore来显示数据,但我似乎遇到了问题。我可以让我的应用程序正常启动并正确显示窗口和对话框,但我无法弄清楚如何以编程方式将数据行添加到List Store并将它们显示在树视图中;我发现大多数教程似乎更喜欢在Glade中创建行,或者通过代码设计整个界面。使用代码,我在运行程序时没有收到任何错误,但我看不到在GtkTreeView中添加任何内容。我也没有看到任何空行添加。

通过编写一些强制生成错误的代码,我可以看到postingButton的信号正在连接(我认为),所以应该没问题。我在GtkTreeView的实际实现中遗漏了一些东西。如果有人可以帮助我,我会非常感激!

编辑:存在的GtkEntries将在稍后实施;我只是想让数据显示出来。

C代码:

/* I'd give objects better names normally, no worries!
 * Just goofing around for now.*/

#include <gtk-3.0/gtk/gtk.h> // Needed for interface.
#include <gtk-3.0/gdk/gdk.h>
#include <glib-2.0/glib-object.h>

void on_mainWindow_destroy(GObject *object, gpointer user_data)
{
    gtk_main_quit();
}

void on_stupidButton_clicked(GtkButton *button, gpointer *user_data)
{
    gtk_dialog_run(GTK_DIALOG(user_data));
}

void on_postingButton_clicked(GtkButton *button, gpointer *user_data)
{
    GtkListStore *liststore1;
    GtkWidget *treeview1; // I've tried defining this as a GtkTreeView as well.
    GtkTreeIter iter;
    GtkBuilder *listStoreBuilder;

    listStoreBuilder = gtk_builder_new();
    gtk_builder_add_from_file(listStoreBuilder,
            "../testing_interface.glade", 0);

    treeview1 = GTK_WIDGET(gtk_builder_get_object(listStoreBuilder,
            "treeview1")); // I'd change this to GTK_TREE_VIEW if the
                           // above is a GtkTreeView.
    liststore1 = GTK_LIST_STORE(
            gtk_tree_view_get_model((GtkTreeView *)treeview1));

    gtk_list_store_append(liststore1, &iter);
    gtk_list_store_set(liststore1, &iter, 0, "c", 1, "d", -1);

    gtk_builder_connect_signals(listStoreBuilder, 0);

    g_object_unref(G_OBJECT(listStoreBuilder));
    g_object_unref(G_OBJECT(liststore1));
}

void on_cancelButton_clicked(GtkButton *button, gpointer *user_data)
{
    gtk_widget_destroy((GtkWidget *)user_data);
}

int main(int argc, char *argv[])
{
    GtkBuilder *builder;
    GtkWidget *mainWindow, *dumbDialog;
    GtkButton *stupidButton;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file(builder, "../testing_interface.glade",
            0);

    mainWindow = GTK_WIDGET(gtk_builder_get_object(builder, "mainWindow"));
    stupidButton = GTK_BUTTON(gtk_builder_get_object(builder, "stupidButton"));
    dumbDialog = GTK_WIDGET(gtk_builder_get_object(builder, "dumbDialog"));

    gtk_builder_connect_signals(builder, 0);

    g_object_unref(G_OBJECT(builder));
    gtk_widget_show(mainWindow);
    gtk_main();

    return 0;
}

Glade文件(testing_interface.glade):

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkDialog" id="dumbDialog">
    <property name="can_focus">False</property>
    <property name="border_width">5</property>
    <property name="type_hint">dialog</property>
    <child internal-child="vbox">
      <object class="GtkBox" id="dialog-vbox1">
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox" id="dialog-action_area1">
            <property name="can_focus">False</property>
            <property name="layout_style">end</property>
            <child>
              <object class="GtkButton" id="postingButton">
                <property name="label" translatable="yes">Post</property>
                <property name="use_action_appearance">False</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_action_appearance">False</property>
                <signal name="clicked" handler="on_postingButton_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="cancelButton">
                <property name="label" translatable="yes">Cancel</property>
                <property name="use_action_appearance">False</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_action_appearance">False</property>
                <signal name="clicked" handler="on_cancelButton_clicked" object="dumbDialog" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="pack_type">end</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkBox" id="box2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkEntry" id="entry1">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="invisible_char">●</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="entry2">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="invisible_char">●</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
    <action-widgets>
      <action-widget response="0">postingButton</action-widget>
      <action-widget response="0">cancelButton</action-widget>
    </action-widgets>
  </object>
  <object class="GtkListStore" id="liststore1">
    <columns>
      <!-- column-name Testing -->
      <column type="gchararray"/>
      <!-- column-name Testing1 -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkWindow" id="mainWindow">
    <property name="can_focus">False</property>
    <signal name="destroy" handler="on_mainWindow_destroy" swapped="no"/>
    <child>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkTreeView" id="treeview1">
            <property name="height_request">183</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="model">liststore1</property>
            <child internal-child="selection">
              <object class="GtkTreeSelection" id="treeview-selection1"/>
            </child>
            <child>
              <object class="GtkTreeViewColumn" id="testing1Col">
                <property name="title" translatable="yes">column</property>
                <child>
                  <object class="GtkCellRendererText" id="cellrenderertext1"/>
                  <attributes>
                    <attribute name="text">0</attribute>
                  </attributes>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkTreeViewColumn" id="testing2Col">
                <property name="title" translatable="yes">column</property>
                <child>
                  <object class="GtkCellRendererText" id="cellrenderertext2"/>
                  <attributes>
                    <attribute name="text">1</attribute>
                  </attributes>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="stupidButton">
            <property name="label" translatable="yes">button</property>
            <property name="use_action_appearance">False</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="use_action_appearance">False</property>
            <signal name="clicked" handler="on_stupidButton_clicked" object="dumbDialog" swapped="no"/>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

1 个答案:

答案 0 :(得分:2)

将行附加到树视图的代码基本上没问题,但是有几个问题会阻止它工作。

  1. 您正在为on_postingButton_clicked的每个条目创建并填充新的构建器。这显然是不正确的,因为这意味着您每次单击“发布”按钮时都会创建一个新树和一个新商店。通过将treeview1作为user_data发送到on_postingButton_clicked回调,可以轻松解决此问题。您已经在dialog中执行此操作,因此我只是修改了XML中的<signal name="clicked" handler="on_postingButton_clicked" ...>以包含object="treeview1"

  2. 你对g_object_unref太自由了。 gtk_tree_view_get_model不会增加其模型的引用计数,因此您不应该取消您获得的liststore。在main中解析构建器可能是一个坏主意,如果你想传递它并从中检索对象(无论如何它都是单例)。

  3. 你应该#include <gtk/gtk.h>,没有干预的gtk-3.0目录。如果您的编译失败,那么您可能没有正确设置pkg-flags

  4. 以下是适用于我的代码的修改版本。它需要如上所述修改XML。

    /* compile with:
       gcc -O2 -Wall glade-test.c $(pkg-config gtk+-3.0 gmodule-export-2.0 --cflags --libs) */
    
    #include <gtk/gtk.h>
    #include <gdk/gdk.h>
    #include <glib-object.h>
    
    void on_mainWindow_destroy(GObject *object, gpointer user_data)
    {
        gtk_main_quit();
    }
    
    void on_stupidButton_clicked(GtkButton *button, gpointer *user_data)
    {
        gtk_dialog_run(GTK_DIALOG(user_data));
    }
    
    void on_postingButton_clicked(GtkButton *button, gpointer *user_data)
    {
        GtkTreeIter iter;
        GtkTreeView *treeview1 = GTK_TREE_VIEW(user_data);
        GtkListStore *liststore1 = GTK_LIST_STORE(gtk_tree_view_get_model(treeview1));
    
        gtk_list_store_append(liststore1, &iter);
        gtk_list_store_set(liststore1, &iter, 0, "c", 1, "d", -1);
    }
    
    void on_cancelButton_clicked(GtkButton *button, gpointer *user_data)
    {
        gtk_widget_destroy((GtkWidget *)user_data);
    }
    
    int main(int argc, char *argv[])
    {
        GtkBuilder *builder;
    
        gtk_init(&argc, &argv);
    
        builder = gtk_builder_new();
        gtk_builder_add_from_file(builder, "testing_interface.glade", 0);
        gtk_builder_connect_signals(builder, 0);
        gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(builder, "mainWindow")));
        g_object_unref(G_OBJECT(builder));
        gtk_main();
    
        return 0;
    }