如何在QTreeWidget中获取选定和取消选择的项目?

时间:2016-03-02 21:44:51

标签: python pyqt4 selection qtreewidget

我有一个树小部件,我正在添加项目。现在,我需要在选择项目时调用自定义过程,或者取消选择之前选择的项目(注意:我正在学习Python和Qt - 后者似乎对我来说太过分了。)

for i in vector:
     parent = QtGui.QTreeWidgetItem(treeWidget)
     parent.setText(0, i[0])
     parent.setText(1, i[1])
     parent.setText(2,i[2])
     parent.setCheckState(0,QtCore.Qt.Unchecked)

1 个答案:

答案 0 :(得分:0)

尝试树小部件选择模型的selectionChanged信号:

        selmodel = self.treeWidget.selectionModel()
        selmodel.selectionChanged.connect(self.handleSelection)
        ...

    def handleSelection(self, selected, deselected):
        for index in selected.indexes():
            item = self.treeWidget.itemFromIndex(index)
            print('SEL: row: %s, col: %s, text: %s' % (
                index.row(), index.column(), item.text(0)))
        for index in deselected.indexes():
            item = self.treeWidget.itemFromIndex(index)
            print('DESEL: row: %s, col: %s, text: %s' % (
                index.row(), index.column(), item.text(0)))