0

我愿意:

  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执行此操作的方法。如何重写树函数?

4

1 回答 1

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!'
于 2013-06-04T23:17:25.310 回答