GTK C Listview动态更新

时间:2013-10-03 08:55:55

标签: ubuntu gcc gtk gtk3

我有一个带有一些文本输入框和一个列表视图的空地。我想根据textentry中输入的值更改listview的值。我可以完成这个吗?我没有找到任何好的教程。

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkAdjustment" id="adjustment1">
    <property name="upper">100</property>
    <property name="step_increment">1</property>
    <property name="page_increment">10</property>
  </object>
  <object class="GtkListStore" id="liststore1">
    <columns>
      <!-- column-name Xaxis -->
      <column type="gint"/>
    </columns>
    <data>
      <row>
        <col id="0">0</col>
      </row>
    </data>
  </object>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <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="GtkScrolledWindow" id="scrolledwindow1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="hadjustment">adjustment1</property>
            <property name="vadjustment">adjustment1</property>
            <property name="shadow_type">in</property>
            <child>
              <object class="GtkTreeView" id="treeview1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="model">liststore1</property>
                <child internal-child="selection">
                  <object class="GtkTreeSelection" id="treeview-selection1"/>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="treeviewcolumn1">
                    <property name="title" translatable="yes">Xaxis</property>
                    <child>
                      <object class="GtkCellRendererSpin" id="cellrendererspin1">
                        <property name="editable">True</property>
                        <property name="adjustment">adjustment1</property>
                      </object>
                      <attributes>
                        <attribute name="text">0</attribute>
                      </attributes>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkGrid" id="grid1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkLabel" id="label1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Value</property>
              </object>
              <packing>
                <property name="left_attach">0</property>
                <property name="top_attach">0</property>
                <property name="width">1</property>
                <property name="height">1</property>
              </packing>
            </child>
            <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="left_attach">1</property>
                <property name="top_attach">0</property>
                <property name="width">1</property>
                <property name="height">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>
  </object>
</interface>

这是林间文件。用于加载林间空地的c文件

#include <gtk/gtk.h>

void on_window_destroy (GtkWidget *object, gpointer user_data) {
    gtk_main_quit();
}

int main (int argc, char *argv[]) {
    GtkBuilder *builder;
    GtkWidget *window;

    gtk_init (&argc, &argv);

    builder = gtk_builder_new ();
    gtk_builder_add_from_file (builder, "test.glade", NULL);

    window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
    g_signal_connect (window, "destroy", G_CALLBACK (on_window_destroy), NULL);

    gtk_widget_show (window);       
    gtk_main ();

    return 0;
}

2 个答案:

答案 0 :(得分:0)

如果我们在发布之前能够看到您尝试的内容,那将会有所帮助......现在我们无法判断您的问题是否是glade,gobject或其中一个GTK小部件。

无论如何,您希望使用gtk_builder_get_object()来查找小部件,然后连接到条目的activate信号或notify信号以获取text属性条目(取决于您是否希望在按Enter键或用户键入时发生事情)。然后在信号处理程序中,您应该以任何您想要修改它的方式修改树视图。

答案 1 :(得分:0)

我希望这会有所帮助。这是你刚刚发布的相同代码。这里定义了一个回调,它将在the entry is activated时调用。回调是 on_entry_activated ,因为您可以看到该条目的值被解析为一个整数,并根据其值附加或替换第一个值。重要的部分是:

  • 将商店对象传递回功能中的回调,以便您稍后更改。
  • 商店中的值( GtkListStore )使用 GtkTreeIter 对象进行操作

作为

#include <gtk/gtk.h>

void on_entry_activated (GtkEntry *entry, gpointer user_data)
{
  gint64 i64;
  gint i;
  GtkListStore *store = (GtkListStore*) user_data;
  GtkTreeIter iter;

  i64 = g_ascii_strtoll (gtk_entry_get_text (entry), NULL, 10);
  i = i64;

  if (i > 0)
    {
      gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter);
    }
  else
    {
      gtk_list_store_append (store, &iter);
    }

  gtk_list_store_set (store, &iter,
              0, i,
              -1);

  gtk_entry_set_text (entry, "");
}

void on_window_destroy (GtkWidget *object, gpointer user_data) {
    gtk_main_quit();
}

int main (int argc, char *argv[]) {
    GtkBuilder *builder;
    GtkWidget *window;

    GtkWidget *entry;
    GtkListStore *liststore1;

    gtk_init (&argc, &argv);

    builder = gtk_builder_new ();
    gtk_builder_add_from_file (builder, "test.glade", NULL);

    window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
    g_signal_connect (window, "destroy", G_CALLBACK (on_window_destroy), NULL);

    entry = GTK_WIDGET (gtk_builder_get_object (builder, "entry1"));
    liststore1 = GTK_LIST_STORE (gtk_builder_get_object (builder, "liststore1"));
    g_signal_connect (entry, "activate", G_CALLBACK (on_entry_activated), liststore1);

    gtk_widget_show (window);
    gtk_main ();

    return 0;
}