qt - 在 QTreeView 中显示来自 QAbstractTableModel 的数据

标签 qt pyqt pyside

短版

我正在尝试在 QAbstracctTableModel 中显示来自 QTreeView 的数据。当我这样做时,我最终将 整个表 显示为每个节点的子节点(以及孙子节点,等等)。如何显示抽象表模型的 TreeView ?

详情

我正在尝试在 QAbstractTableModel 中的 QTreeView 中显示一些数据。在 Model-View Tutorial 中,在展示了一个示例 QAbstractTableModel 之后,它看起来就像用 QTableView 替换 QTreeView 一样简单:

You can convert the example above into an application with a tree view. Simply replace QTableView with QTreeView, which results in a read/write tree. No changes have to be made to the model.

当我进行这个替换时,我最终会显示一个树,但是如果我单击任何图标来展开它(这应该什么都不做,因为没有内置层次结构),Python 会崩溃并出现 Python.exe has stopped working 。这个 has been brought up before ],但没有可行的解决方案。

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

bad tree

我似乎陷入了某种递归的噩梦,不知道如何逃脱。下面的相关问题表明我可能不得不去 QAbstractItemModel ,但上面的教程引用却提出了其他建议(其中指出,不必对模型进行任何更改)。

相关问题

QTreeView always displaying the same data

完整的工作示例

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()

最佳答案

来自 official documentation :

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

columnCount() 也是如此。为了完整起见,如果父级有效,data() 应该返回 None


事情是这样的:

  1. 您点击“Stinkberries”旁边的“+”号。
  2. QTreeView 认为,“我需要扩展 View 。我想知道 'Stinkberries' 下有多少行?” 为了找出答案,QTreeView 调用 rowCount(),并将“Stinkberries”单元格的索引作为父级传递。
  3. FavoritesTableModel::rowCount() 返回 5,所以 QTreeView 认为,“啊,'Stinkberries' 下有 5 行。”
  4. 列也会发生同样的过程。
  5. QTreeView 决定检索“Stinkberries”下的第一项。它调用 data(),将第 0 行、第 0 列和“Stinkberries”单元格的索引作为父单元传递。
  6. FavoritesTableModel::data() 返回“Apples”,所以 QTreeView 认为,“啊,'Stinkberries' 下的第一项是'Apples'。”
  7. 等等

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


最后,为了确保“+”号根本不出现,让 hasChildren() 为每个单元格返回 false。

关于qt - 在 QTreeView 中显示来自 QAbstractTableModel 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27308309/

相关文章:

python - 用Python开发GUI:Tkinter与PyQt

python - 将 Acrobat Reader 嵌入 PyQt5 应用程序会导致空白页面 [PyQt5]

python - PySide QGraphicsView 大小

python - QTableView 中的虚拟列?

ios - QTWebView真的可以通过Apple App Store检查吗?

python - 来自错误模型的 PyQt QSortFilterProxyModel 索引传递给 mapToSource?

c++ - qPrintable 是否容易出现访问冲突?

python - 删除 QGraphicsItem 周围的虚线选择

c++ - 使用CMake的Qt moc错误

c++ - 让 qmake 正确运行我的代码生成器