重写QTreeWidget>>>带有复选框的QTreeView?

时间:2013-06-02 13:52:14

标签: pyqt4 qtreeview qtreewidget

我做:

  class Window(QtGui.QWidget):

    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.modelTree = QtGui.QTreeView()
        self.model = QtGui.QStandardItemModel()
        self.addItems(self.model, data)
        self.modelTree.setModel(self.model)
        self.modelTree.connect(self.modelTree, QtCore.SIGNAL('clicked(QModelIndex)'), self.treefunction)
    def treefunction(self, index):
        print index.model().itemFromIndex(index).text()

        '''if item.checkState(column) == QtCore.Qt.Checked:
            print "checked", item.text(column)
        if item.checkState(column) == QtCore.Qt.Unchecked:
            print "NOT checked",item.text(column)'''

    def addItems(self, parent,elements):
        column = 0
        clients_item = QtGui.QTreeWidgetItem(parent, ['Serwis'])
        clients_item.setData(column, QtCore.Qt.UserRole, 'serwis 111')
        clients_item.setExpanded(True)


        item_1 = QtGui.QTreeWidgetItem(clients_item, ['Wartswa1'])
        item_1.setData(column, QtCore.Qt.UserRole, 'Wasrtwas 1')
        item_1.setCheckState(column, QtCore.Qt.Unchecked)

        item_2 = QtGui.QTreeWidgetItem(clients_item, ['Wartswa2'])
        item_2.setData(column, QtCore.Qt.UserRole, 'Wasrtwas 2')
        item_2.setCheckState(column, QtCore.Qt.Unchecked)

如果选中或取消选中行,我想获取信息。我找到了一个例子,但是对于QTreeWidget。我希望用 QTreeView 来做到这一点。如何重写树函数?

1 个答案:

答案 0 :(得分:1)

您要找的是Qt::CheckStateRole

treefunction中,执行:

if self.model.data(index, Qt.CheckStateRole) == Qt.Checked:
    print 'Checked!'
elif self.model.data(index, Qt.CheckStateRole) == Qt.Unchecked:
    print 'Unchecked!'