0

假设我有一个代表产品列表的 TreeView(带有支持 TreeStore)。

产品具有以下规格:

  1. 姓名
  2. 目录编号
  3. 评论

此外,每个产品都有一个构成它的组件列表。我想在树的子节点中显示有关组件的数据(基本上是一个 2 级树视图),但是顶级节点将具有与子节点不同的模型。

如何使用 Glade(或者如果需要,通过 Python+PyGObjecy)完成它?

4

1 回答 1

2

每个 TreeView 只能有一个模型,但您可以制作一个实际上是两个(或多个)模型粘合在一起的模型,并添加一些布尔列来控制该行应该显示哪个模型。然后,添加一组 CellRenderers 并将其可见性映射到这些控制列。

这样做的困难在于,当您希望列的名称显示在顶部时,在这种情况下可能很难对信息进行排序。如果您可以对您希望树的外观进行线框图,我可以提供一个基本示例实现。

== 编辑 ==

这是一个例子:

主.py

from gi.repository import Gtk
from os.path import abspath, dirname, join

WHERE_AM_I = abspath(dirname(__file__))

# My Model map
mm = {
    'name'        : 0,
    'catalog_num' : 1,
    'comment'     : 2,
    'component'   : 3,
    'is_top'      : 4,
    'is_child'    : 5,
}

class MyApp(object):

    def __init__(self):
        """
        Build GUI
        """

        # Build GUI from Glade file
        self.builder = Gtk.Builder()
        self.glade_file = join(WHERE_AM_I, 'gui.glade')
        self.builder.add_from_file(self.glade_file)

        # Get objects
        go = self.builder.get_object
        self.window = go('window')
        self.treestore = go('treestore')

        # Fill model
        self._load_model()

        # Connect signals
        self.builder.connect_signals(self)
        self.window.connect('delete-event', lambda x,y: Gtk.main_quit())

        # Everything is ready
        self.window.show()


    def _load_model(self):

        my_data = [
                ['Cheese', 'F001', 'This is the best cheese ever!', '', True, False],
                ['Pepperoni', 'F002', 'Delicious pepperoni :}', '', True, False],
                ['Pepperonni Pizza', 'P001', 'Yes, I\'m hungry :(', '', True, False],
                ['', '', None, 'Cheese', False, True],
                ['', '', None, 'Pepperonni', False, True],
            ]

        parent = None
        for i in my_data:
            if i[mm['is_child']]:
                self.treestore.append(parent, i)
            else:
                parent = self.treestore.append(None, i)


if __name__ == '__main__':
    gui = MyApp()
    Gtk.main()

gui.glade

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkTreeStore" id="treestore">
    <columns>
      <!-- column-name name -->
      <column type="gchararray"/>
      <!-- column-name catalog_num -->
      <column type="gchararray"/>
      <!-- column-name comment -->
      <column type="gchararray"/>
      <!-- column-name component -->
      <column type="gchararray"/>
      <!-- column-name is_top -->
      <column type="gboolean"/>
      <!-- column-name is_child -->
      <column type="gboolean"/>
    </columns>
  </object>
  <object class="GtkWindow" id="window">
    <property name="can_focus">False</property>
    <property name="border_width">10</property>
    <property name="title" translatable="yes">TreeView test</property>
    <property name="window_position">center-always</property>
    <property name="default_width">400</property>
    <property name="default_height">300</property>
    <child>
      <object class="GtkScrolledWindow" id="scrolledwindow">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="shadow_type">in</property>
        <child>
          <object class="GtkTreeView" id="treeview">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="model">treestore</property>
            <property name="headers_visible">False</property>
            <property name="headers_clickable">False</property>
            <property name="search_column">1</property>
            <property name="tooltip_column">2</property>
            <child internal-child="selection">
              <object class="GtkTreeSelection" id="treeview-selection"/>
            </child>
            <child>
              <object class="GtkTreeViewColumn" id="treeviewcolumn_name">
                <property name="title" translatable="yes">Name</property>
                <child>
                  <object class="GtkCellRendererText" id="cellrenderertext_name"/>
                  <attributes>
                    <attribute name="visible">4</attribute>
                    <attribute name="text">0</attribute>
                  </attributes>
                </child>
                <child>
                  <object class="GtkCellRendererText" id="cellrenderertext_component"/>
                  <attributes>
                    <attribute name="visible">5</attribute>
                    <attribute name="text">3</attribute>
                  </attributes>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkTreeViewColumn" id="treeviewcolumn_catalog_num">
                <property name="title" translatable="yes">Catalog #</property>
                <child>
                  <object class="GtkCellRendererText" id="cellrenderertext_catalog"/>
                  <attributes>
                    <attribute name="visible">4</attribute>
                    <attribute name="text">1</attribute>
                  </attributes>
                </child>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

代码也可以在以下位置找到:https ://gist.github.com/carlos-jenkins/5555283

于 2013-05-10T15:08:44.977 回答