0

我对一些文本输入框和一个列表视图感到满意。我想根据 textentry 中输入的值更改列表视图的值。我该如何完成这个?我没有找到任何好的教程。

<?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">•&lt;/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>

这是glade文件。用于加载glade文件的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;
}
4

2 回答 2

0

我希望这有帮助。这与您发布的代码相同,只是稍作调整。这里定义了一个回调,当条目被激活时将被调用。回调是on_entry_activated,您可以看到条目的值被解析为整数,并根据其值附加或替换第一个值。重要的部分是:

  • store 对象在main函数中传递给回调,以便您以后可以更改它。
  • 存储区(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;
}
于 2013-10-04T13:30:31.750 回答
0

如果我们能看到您在此处发布之前尝试过的事情会有所帮助...现在我们无法判断您的问题是 glade、gobject 还是 GTK 小部件之一。

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

于 2013-10-04T07:36:06.297 回答