对GtkTreeView中的行进行排序后重新排序

时间:2018-02-13 20:38:26

标签: c gtk glade gtk2 gtktreeview

我有以下GTK +程序,它使用Glade生成的XML文件来构建其GUI。

#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>

////////////////////////////////////////////////////
// Example can be compiled with:
// gcc gui.c `pkg-config --cflags --libs gtk+-2.0`
//
// GUI file from glade must be in same folder from
// which the compiled binary is called.
////////////////////////////////////////////////////
int main(int argc, char* argv[]) {
    GtkBuilder* gtkBuilder = gtk_builder_new();

    gtk_init(&argc, &argv);

    gtk_builder_add_from_file(gtkBuilder, "gui.glade", NULL);

    GtkTreeStore* treestore = GTK_TREE_STORE(gtk_builder_get_object(gtkBuilder, "treestore"));

    GtkTreeIter iterChild;

    gtk_tree_store_append(treestore, &iterChild, NULL);
    gtk_tree_store_set(treestore, &iterChild, 0, "a", -1);
    gtk_tree_store_append(treestore, &iterChild, NULL);
    gtk_tree_store_set(treestore, &iterChild, 0, "b", -1);
    gtk_tree_store_append(treestore, &iterChild, NULL);
    gtk_tree_store_set(treestore, &iterChild, 0, "m", -1);
    gtk_tree_store_append(treestore, &iterChild, NULL);
    gtk_tree_store_set(treestore, &iterChild, 0, "k", -1);
    gtk_tree_store_append(treestore, &iterChild, NULL);
    gtk_tree_store_set(treestore, &iterChild, 0, "g", -1);

    gtk_builder_connect_signals(gtkBuilder, NULL);

    gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "main")));

    g_object_unref(G_OBJECT(gtkBuilder));

    gtk_main();

    return 0;
}

XML Glade文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="2.24"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkTreeStore" id="treestore">
    <columns>
      <!-- column-name col -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkWindow" id="main">
    <property name="can_focus">False</property>
    <property name="default_width">690</property>
    <property name="default_height">500</property>
    <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
    <child>
      <object class="GtkTreeView" id="treeview">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="model">treestore</property>
        <property name="reorderable">True</property>
        <property name="level_indentation">1</property>
        <property name="enable_tree_lines">True</property>
        <child>
          <object class="GtkTreeViewColumn" id="treeviewcolumn">
            <property name="sizing">fixed</property>
            <property name="title" translatable="yes">column</property>
            <property name="clickable">True</property>
            <property name="sort_indicator">True</property>
            <property name="sort_column_id">0</property>
            <child>
              <object class="GtkCellRendererText" id="treeviewrenderer">
                <property name="height">30</property>
              </object>
              <attributes>
                <attribute name="text">0</attribute>
              </attributes>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

当应用程序启动时,可以通过Drag'n'Drop重新排序行。单击列标题后,将对行进行排序。之后他们再也不能被Drag'n'Drop重新排列了。

在对行进行排序后,有没有办法重新激活手动重新排序?

我尝试过,例如,在回调函数中调用gtk_tree_view_set_reorderable()(回调为on_cursor_moved()),但这不起作用。

使用Glade XML文件构建GUI时,是否可以在已排序和可重新排序的行之间切换?

1 个答案:

答案 0 :(得分:1)

您需要使用treesort包装树模型,然后每三次单击禁用排序。

Glade文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.0"/>
  <object class="GtkTreeStore" id="treestore">
    <columns>
      <!-- column-name col -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkTreeModelSort" id="treesort">
    <property name="model">treestore</property>
  </object>
  <object class="GtkWindow" id="main">
    <property name="can_focus">False</property>
    <property name="default_width">690</property>
    <property name="default_height">500</property>
    <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
    <child>
      <object class="GtkTreeView" id="treeview">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="model">treesort</property>
        <property name="reorderable">True</property>
        <property name="level_indentation">1</property>
        <property name="enable_tree_lines">True</property>
        <child internal-child="selection">
          <object class="GtkTreeSelection" id="treeview-selection1"/>
        </child>
        <child>
          <object class="GtkTreeViewColumn" id="treeviewcolumn">
            <property name="sizing">fixed</property>
            <property name="title" translatable="yes">column</property>
            <property name="clickable">True</property>
            <property name="sort_indicator">True</property>
            <property name="sort_column_id">0</property>
            <child>
              <object class="GtkCellRendererText" id="treeviewrenderer">
                <property name="height">30</property>
              </object>
              <attributes>
                <attribute name="text">0</attribute>
              </attributes>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>