返回QTreeview选择Pyside的热门产品

时间:2017-02-02 17:23:10

标签: python pyside qtreeview

我有一个QTreeview,根据用户选择,我想获得一个包含所选项目父母姓名的唯一数组。

因此,如果选择了任何子项,它将返回父项,如果选择了父项,它仍将返回父项。

想要退回:

>> [Kevin, Michelle, Nikki, Tim]

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.treeview.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)

        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):
        parents = [
            Person("Kevin", [Person("Tom"), Person("Sarah"), Person("Chester")]),
            Person("Michelle", [Person("James"), Person("Corey"),Person("Leslie")]),
            Person("Doug", [Person("Fred"), Person("Harold"),Person("Stephen")]),
            Person("Nikki", [Person("Brody"), Person("Tyson"),Person("Bella")]),
            Person("Tim", [Person("Marie"), Person("Val"),Person("Ted")])
        ]

        for p in parents:
            self.create_nodes(p, 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 parents..."


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

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:2)

这是一个简单的实现:

def selectedParents(self):
    parents = set()
    for index in self.treeview.selectedIndexes():
        while index.parent().isValid():
            index = index.parent()
        parents.add(index.sibling(index.row(), 0))
    return [index.data() for index in sorted(parents)]

请注意selectedIndexes()按照选择的顺序返回索引,如果有多列,则包含所选行中每列的索引。

因此,上述方法确保仅包含第一列中的文本,并确保以正确的顺序返回项目。