PySide (PyQt) QAbstractItemModel



我正试图实现一个简单的模型的树视图,我不知道为什么它不工作。似乎我所有的节点都是我的节点的子节点,尽管其中三个应该是它的第一个子节点的子节点。我还可以看到我的hasChildren方法进入了一个无限循环。如有任何帮助,我将不胜感激。

下面是我的代码:
#!/usr/bin/env python2
from PySide import QtGui, QtCore
import sys
class SI(object):
    children = []
    def __init__(self, group=None, parent=None):
        self.parent = parent
        self.group = group
    def data(self, column):
        return self.group[column]
    def appendChild(self, group):
        self.children.append(SI(group, self))
    def child(self, row):
        return self.children[row]
    def childrenCount(self):
        return len(self.children)
    def hasChildren(self):
        if len(self.children) > 0 :
            return True
        return False
    def row(self):
        if self.parent:
            return self.parent.children.index(self)
        return 0
    def columnCount(self):
        return len(self.group)
class SM(QtCore.QAbstractItemModel):
    root = SI(["First", "Second"])
    def __init__(self, parent=None):
        super(SM, self).__init__(parent)
        self.createData()
    def createData(self):
        for x in [["a", "A"], ["b","B"], ["c", "C"]]:
            self.root.appendChild(x)
        for y in [["aa", "AA"], ["ab", "AB"], ["ac","AC"]]:
            self.root.child(0).appendChild(y)
    def columnCount(self, index=QtCore.QModelIndex()):
        if index.isValid():
            return index.internalPointer().columnCount()
        else:
            return self.root.columnCount()
    def rowCount(self, index=QtCore.QModelIndex()):
        if index.row() > 0:
            return 0
        if index.isValid():
            item = index.internalPointer()
        else:
            item = self.root
        return item.childrenCount()
    def index(self, row, column, index=QtCore.QModelIndex()):
        if not self.hasIndex(row, column, index):
            return QtCore.QModelIndex()
        if not index.isValid():
            item = self.root
        else:
            item = index.internalPointer()
        child = item.child(row)
        if child:
            return self.createIndex(row, column, child)
        return QtCore.QMOdelIndex()
    def parent(self, index):
        if not index.isValid():
            return QtCore.QModelIndex()
        item = index.internalPointer()
        if not item:
            return QtCore.QModelIndex()
        parent = item.parent
        if parent == self.root:
            return QtCore.QModelIndex()
        else:
            return self.createIndex(parent.row(), 0, parent)
    def hasChildren(self, index):
        if not index.isValid():
            item = self.root
        else:
            item = index.internalPointer()
        return item.hasChildren()
    def data(self, index, role=QtCore.Qt.DisplayRole):
       if index.isValid() and role == QtCore.Qt.DisplayRole:
            return index.internalPointer().data(index.column())
       elif not index.isValid():
            return self.root.getData()
    def headerData(self, section, orientation, role):
        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return self.root.data(section)
class MyTree(QtGui.QTreeView):
    def __init__(self, parent=None, model=SM):
        super(MyTree, self).__init__(parent)
        self.setModel(model())

class Window(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.initGui()
    def initGui(self):
       vlo = QtGui.QVBoxLayout()
       tree = MyTree(self)
       vlo.addWidget(tree)
       self.setLayout(vlo)
       self.show()
def main():
    app = QtGui.QApplication(sys.argv)
    win = Window()
    exit(app.exec_())
if __name__ == '__main__':
    main()

嗯,除了一个(奇怪的!)错误之外,我认为你几乎已经明白了。您将children声明为SI中的类级别变量,而它应该是一个实例变量,因为每个SI都可以有孩子。你不希望一个列表包含所有的实例。

所以我把第一行改成:

class SI(object):

   def __init__(self, group=None, parent=None):
       self.parent = parent
       self.group = group
       self.children = []
   def data(self, column):
       return self.group[column]

,看起来一切正常。当然,我还没有详细测试过,但这应该可以让您了解。

在实现中有一个错误:

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

代替:

if index.row() > 0:

应该有:

if index.column() > 0:

来源:http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

相关内容

  • 没有找到相关文章

最新更新