无法选择 TreeView 中的复选框 [英] Unable to select Checkbox inside TreeView
问题描述
我尝试使用复选框创建树视图,但无法选择复选框.
I tried creating a treeview with checkboxes but I'm unable to select the checkboxes.
在标记方法中,我曾将其称为 ItemisuserCheckable 但仍然无法使其正常工作...
on the flag method I had mentioned it as ItemisuserCheckable but still could not get it working...
我在这里是否缺少启用复选框选择的功能.
am I missing something here to enable the selection of checkboxes.
代码片段是:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class StbTreeView(QAbstractListModel):
def __init__(self, args, parent=None):
super(StbTreeView, self).__init__(parent)
self.args = args
print self.args
def rowCount(self, parent):
return len(self.args)
def headerData(self, section, orientation, role):
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return QString("Select STB's")
def flags(self, index):
row = index.row()
if row:
return Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsSelectable
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
row = index.row()
return self.args[row]
if role == Qt.CheckStateRole:
row = index.row()
return QVariant(Qt.Unchecked)
def setData(self, index, value, role):
if role == Qt.CheckStateRole:
if value == Qt.Checked:
row = index.row()
selected_stb = self.args[row]
print 'selected_stb is %s' % selected_stb
print 'Value is %s' % value
self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),index, index)
return True
#return QVariant(Qt.Checked)
def main():
myapp = QApplication(sys.argv)
data = ['STB1', 'STB2', 'STB3', 'STB4', 'STB5', 'STB6', 'STB7', 'STB8']
model = StbTreeView(data)
tree_view = QTreeView()
tree_view.show()
tree_view.setModel(model)
myapp.exec_()
if __name__ == '__main__':
main()
推荐答案
您需要在某个地方保存当前项目状态(选中\未选中),并在调用 setdata() 方法后更改它.您的项目始终未选中,因为您总是在 data() 方法中为它们返回 QVariant(Qt.Unchecked)
.
you need to hold somewhere current item state (checked\unchecked) and change it once setdata() method is called. Your items are always unchecked because you're always returning QVariant(Qt.Unchecked)
for them in the data() method.
我对您的代码进行了一些更改,看看它是否适合您:
I've changed a bit your code, see it would work for you:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class TestItem():
def __init__(self, name, checked):
self.checked = checked
self.name = name
class StbTreeView(QAbstractListModel):
def __init__(self, args, parent=None):
super(StbTreeView, self).__init__(parent)
self.args = []
for item_name in args:
self.args.append(TestItem(item_name, False))
for item in self.args:
print item.name
def rowCount(self, parent):
return len(self.args)
def headerData(self, section, orientation, role):
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return QString("Select STB's")
def flags(self, index):
return Qt.ItemIsUserCheckable | Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsEnabled
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
row = index.row()
print self.args[row].name
return self.args[row].name
if role == Qt.CheckStateRole:
row = index.row()
print self.args[row].checked
if self.args[row].checked == False:
return QVariant(Qt.Unchecked)
else:
return QVariant(Qt.Checked)
def setData(self, index, value, role):
if role == Qt.CheckStateRole:
row = index.row()
self.args[row].checked = not self.args[row].checked
return True
def main():
myapp = QApplication(sys.argv)
data = ['STB1', 'STB2', 'STB3', 'STB4', 'STB5', 'STB6', 'STB7', 'STB8']
model = StbTreeView(data)
tree_view = QTreeView()
tree_view.show()
tree_view.setModel(model)
myapp.exec_()
if __name__ == '__main__':
main()
希望对您有所帮助,问候
hope this helps, regards
这篇关于无法选择 TreeView 中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!