用复选框重写 QTreeWidget >>> QTreeView?
Posted
技术标签:
【中文标题】用复选框重写 QTreeWidget >>> QTreeView?【英文标题】:Rewrite QTreeWidget >>> QTreeView with checkbox? 【发布时间】:2013-06-02 13:52:14 【问题描述】:我愿意:
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 的方法。如何重写treefunction?
【问题讨论】:
【参考方案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!'
【讨论】:
以上是关于用复选框重写 QTreeWidget >>> QTreeView?的主要内容,如果未能解决你的问题,请参考以下文章