在QTreeView中显示来自QAbstractTableModel的数据

埃里克

精简版

我试图从显示的数据QAbstracctTableModelQTreeView完成后,最终将整个表显示为每个节点的子代(以及孙子代,等等)。如何显示抽象表模型的树状视图?

细节

我想在显示一些数据QAbstractTableModelQTreeView模型-视图教程,呈现一个例子后QAbstractTableModel,这使它看起来很简单的替换QTableViewQTreeView

您可以将上面的示例转换为具有树视图的应用程序。只需将QTableView替换为QTreeView,即可生成一个读/写树。无需对模型进行任何更改。

进行此替换时,最终会显示一棵树,但是如果单击任何图标以将其展开(由于没有内置层次结构,该图标将不起作用),Python崩溃Python.exe has stopped working是在]之前提出的,但是没有可行的解决方案。

为了尝试解决此问题,我在QAbstractTableModel子类中重新实现了索引功能(请参见下面的完整工作示例)。这导致了非常不同的错误类型。即,树中的每个节点现在都包含整个表作为数据。无论我单击多少次,都将显示整个表格。像这样:

坏树

I seem to be in some kind of recursive nightmare, and do not know how to escape. The related question below suggests I might have to go to QAbstractItemModel, but the tutorial quote above suggests otherwise (which states, No changes have to be made to the model).

Related question

QTreeView always displaying the same data

Full working example

from PySide import QtGui, QtCore

class Food(object):
    def __init__(self, name, shortDescription, note, parent = None):
        self.data = (name, shortDescription, note);
        self.parentIndex = parent

class FavoritesTableModel(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)
        self.foods = []  
        self.loadData() 

    def data(self, index, role = QtCore.Qt.DisplayRole):
        if role == QtCore.Qt.DisplayRole:
            return self.foods[index.row()].data[index.column()]
        return None

    def rowCount(self, index=QtCore.QModelIndex()):
        return len(self.foods)

    def columnCount(self, index=QtCore.QModelIndex()):
        return 3

    def index(self, row, column, parent = QtCore.QModelIndex()):  
        return self.createIndex(row, column, parent)

    def loadData(self):   
        allFoods=("Apples", "Pears", "Grapes", "Cookies", "Stinkberries")
        allDescs = ("Red", "Green", "Purple", "Yummy", "Huh?")
        allNotes = ("Bought recently", "Kind of delicious", "Weird wine grapes",
                    "So good...eat with milk", "Don't put in your nose")
        for name, shortDescription, note in zip(allFoods, allDescs, allNotes):
            food = Food(name, shortDescription, note)                                      
            self.foods.append(food) 

def main():
    import sys
    app = QtGui.QApplication(sys.argv)

    model = FavoritesTableModel() 

    #Table view
    view1 = QtGui.QTableView()
    view1.setModel(model)
    view1.show()

    #Tree view
    view2 = QtGui.QTreeView()
    view2.setModel(model)
    view2.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
JKSH

From the official documentation:

When implementing a table based model, rowCount() should return 0 when the parent is valid.

The same goes for columnCount(). And for completeness, data() should return None if the parent is valid.


What happened is this:

  1. You click on the '+' sign next to "Stinkberries".
  2. The QTreeView thinks, "I need to expand the view. I wonder how many rows exist under 'Stinkberries'?" To find out, the QTreeView calls rowCount(), and passes the index of the "Stinkberries" cell as the parent.
  3. FavoritesTableModel::rowCount() returns 5, so the QTreeView thinks, "Ah, there are 5 rows under 'Stinkberries'."
  4. The same process happens for columns.
  5. The QTreeView decides to retrieve the first item under "Stinkberries". It calls data(), passing Row 0, Column 0, and the index of the "Stinkberries" cell as the parent.
  6. FavoritesTableModel::data() returns "Apples", so the QTreeView thinks, "Ah, the first item under 'Stinkberries' is 'Apples'."
  7. etc.

为了获得正确的行为,您的代码必须为步骤3和步骤4返回0。


最后,要确保根本不显示“ +”号,请hasChildren()对每个单元格返回false。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

重新实现QAbstractTableModel后,TableView在表中无数据显示

来自分类Dev

在Angularjs中显示来自Json的数据

来自分类Dev

在Ionic中显示来自后端的数据

来自分类Dev

显示来自jquery jtable中搜索的数据

来自分类Dev

在html中显示来自json数据的图像?

来自分类Dev

无法在离子中显示来自帖子的数据

来自分类Dev

在QStandardItemModel / QTreeView中处理数据?

来自分类Dev

显示来自php中复杂json数据的数据

来自分类Dev

解析来自 JSON 的数据后,Recyclerview 中的数据未显示

来自分类Dev

显示来自 json 的数据

来自分类Dev

来自Firebase的数据未显示在片段中的RecyclerView中

来自分类Dev

在QTreeView中以树形显示文件列表

来自分类Dev

PyQt-实现一个QAbstractTableModel以在QTableView中显示

来自分类Dev

来自http-mock的数据未显示在模板中

来自分类Dev

在Chrome中获取并显示来自Serviceworker的推送数据

来自分类Dev

MethodNotFoundException-在JSP表中显示来自SQL的数据

来自分类Dev

来自指令的数据未在ng-repeat中显示

来自分类Dev

在日志中显示来自SQLite数据库的ArrayList

来自分类Dev

无法在QML TableView中显示来自QSqlQueryModel的数据

来自分类Dev

Datagrid不显示来自ObservableCollection的MVVM中的数据

来自分类Dev

无法使用React在流星中显示来自Mongo的数据

来自分类Dev

在API中显示来自手风琴的数据

来自分类Dev

在Angular中显示来自Rest Api的数据的正确方法

来自分类Dev

在Angular 6中显示来自api的数据的问题

来自分类Dev

如何在React Native中水平显示来自API的数据

来自分类Dev

如何在React Native中显示来自Firebase Firestore的数据

来自分类Dev

如何在React表中显示来自Mysql的数据

来自分类Dev

如何在React中显示来自api的数据

来自分类Dev

在V-CALENDAR Vuetify Vuejs中显示来自Api的数据

Related 相关文章

  1. 1

    重新实现QAbstractTableModel后,TableView在表中无数据显示

  2. 2

    在Angularjs中显示来自Json的数据

  3. 3

    在Ionic中显示来自后端的数据

  4. 4

    显示来自jquery jtable中搜索的数据

  5. 5

    在html中显示来自json数据的图像?

  6. 6

    无法在离子中显示来自帖子的数据

  7. 7

    在QStandardItemModel / QTreeView中处理数据?

  8. 8

    显示来自php中复杂json数据的数据

  9. 9

    解析来自 JSON 的数据后,Recyclerview 中的数据未显示

  10. 10

    显示来自 json 的数据

  11. 11

    来自Firebase的数据未显示在片段中的RecyclerView中

  12. 12

    在QTreeView中以树形显示文件列表

  13. 13

    PyQt-实现一个QAbstractTableModel以在QTableView中显示

  14. 14

    来自http-mock的数据未显示在模板中

  15. 15

    在Chrome中获取并显示来自Serviceworker的推送数据

  16. 16

    MethodNotFoundException-在JSP表中显示来自SQL的数据

  17. 17

    来自指令的数据未在ng-repeat中显示

  18. 18

    在日志中显示来自SQLite数据库的ArrayList

  19. 19

    无法在QML TableView中显示来自QSqlQueryModel的数据

  20. 20

    Datagrid不显示来自ObservableCollection的MVVM中的数据

  21. 21

    无法使用React在流星中显示来自Mongo的数据

  22. 22

    在API中显示来自手风琴的数据

  23. 23

    在Angular中显示来自Rest Api的数据的正确方法

  24. 24

    在Angular 6中显示来自api的数据的问题

  25. 25

    如何在React Native中水平显示来自API的数据

  26. 26

    如何在React Native中显示来自Firebase Firestore的数据

  27. 27

    如何在React表中显示来自Mysql的数据

  28. 28

    如何在React中显示来自api的数据

  29. 29

    在V-CALENDAR Vuetify Vuejs中显示来自Api的数据

热门标签

归档