gpt4 book ai didi

qt - PySide (PyQt) QAbstractItemModel

转载 作者:行者123 更新时间:2023-12-04 17:52:27 30 4
gpt4 key购买 nike

我正在尝试为 TreeView 实现一个简单的模型,但我不知道为什么它不起作用。似乎我所有的节点都是我的 的子节点。根节点,尽管其中三个应该是它的子节点 第一 child 。我也可以看到我的 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可以生 child 。您不希望所有实例都有一个列表。

所以我将第一行改为:

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]

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

关于qt - PySide (PyQt) QAbstractItemModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14075332/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com