5

我正在尝试为树视图实现一个简单的模型,但我不知道它为什么不起作用。似乎我的所有节点都是我的根节点的子节点,尽管其中三个应该是它的第一个子节点的子节点。此外,我可以看到我的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()
4

2 回答 2

7

嗯,我认为除了一个(奇怪的!)错误之外,您几乎已经掌握了它。您声明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]

一切似乎都正常。当然,我还没有详细测试过,但这应该会让你继续前进。

于 2013-05-16T08:25:14.723 回答
1

执行中有错误:

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

于 2015-06-15T22:43:01.650 回答