python - python中递归收集所有选中的QTreeview项

标签 python qt search pyside qtreeview

有人可以帮我创建一个递归函数,它循环遍历 TreeView QStandardItemModel 并收集所有“检查为真”的项目

我不太清楚如何自己去做这件事。

enter image description here

from PySide import QtGui, QtCore
from PySide import QtSvg, QtXml
import sys

class Person:
    def __init__(self, name="", children=None):
        self.name = name
        self.children = children if children else []

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.resize(300, 400)
        self.init_ui()

    def init_ui(self):
        # Setup Tabs Widget
        # self.treeview = QtGui.QTreeView()
        self.treeview = QtGui.QTreeView()
        self.treeview.setHeaderHidden(True)
        self.treeview.setUniformRowHeights(True)
        self.treeview.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)

        self.model = QtGui.QStandardItemModel()
        self.treeview.setModel(self.model)

        self.action = QtGui.QAction('Print', self)
        self.action.setShortcut('F5')
        self.action.triggered.connect(self.get_checked)

        fileMenu = QtGui.QMenu("&File", self)
        fileMenu.addAction(self.action)
        self.menuBar().addMenu(fileMenu)

        # Setup central widget
        self.setCentralWidget(self.treeview)

        # populate data
        self.populate_people()
        self.treeview.expandAll()

    def populate_people(self):
        parent = Person("Kevin", [
                Person("Tom", [Person("Sally"), Person("Susan")]),
                Person("Snappy", [Person("John"), Person("Kimmy"),
                                  Person("Joe")]),
                Person("Chester", [Person("Danny"), Person("Colleen")])
            ]
        )
        self.create_nodes(parent, self.model)

    def create_nodes(self, node, parent):
        tnode = QtGui.QStandardItem()
        tnode.setCheckable(True)
        tnode.setData(QtCore.Qt.Unchecked, role=QtCore.Qt.CheckStateRole)
        tnode.setData(node.name , role=QtCore.Qt.DisplayRole)
        tnode.setData(node, role=QtCore.Qt.UserRole) # store object on item

        parent.appendRow(tnode)

        for x in node.children:
            self.create_nodes(x, tnode)

    def get_checked(self):
        print "collecting..."


def main():
    app = QtGui.QApplication(sys.argv)
    ex = MainWindow()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

最佳答案

可以通过模型的 match 来完成方法:

def get_checked(self):
    model = self.treeview.model()
    checked = model.match(
        model.index(0, 0), QtCore.Qt.CheckStateRole,
        QtCore.Qt.Checked, -1,
        QtCore.Qt.MatchExactly | QtCore.Qt.MatchRecursive)
    for index in checked:
        item = model.itemFromIndex(index)
        print(item.text())

关于python - python中递归收集所有选中的QTreeview项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40614783/

相关文章:

python - 使用 Python 请求响应原始文件类对象(处理 pcap 文件而不将其保存到磁盘)

python - 使用 Python/Elementtree 将具有相同标签的多个子元素添加到 en XML 树中

python - 当列表中的元组没有返回任何内容时写入错误

c++ - Qt - DLL 中的对话框

c++ - 为什么Webkit运行时pow()计算错误?

search - 推荐一个简单的词袋搜索引擎?

python - GS响应错误: GSResponseError: 403 Forbidden while writing to Bucket

c++ - 在信号中发出指针列表 - 如何处理删除

mysql - 如何在MySql中搜索所有以数字开头的字符串?

python - 如何在python字符串中找到相反的字符?