I want to display a QListView where each item is a checkbox with some label. The checkboxes should be visible at all times. One way I can think of is using a custom delegate and QAbstractListModel. Are there simpler ways? Can you provide the simplest snippet that does this?
我想显示一个QListView,其中每个条目都是一个带有标签的复选框。复选框应该始终可见。我能想到的一种方法是使用自定义委托和QAbstractListModel。有更简单的方法吗?你能提供最简单的代码片段吗?
Thanks in advance
谢谢提前
2 个解决方案
#1
10
If you are writing your own model, just include the Qt.ItemIsUserCheckable
flag in the return value from the flags()
method, and ensure that you return a valid value for the Qt.CheckStateRole
from the data()
method.
如果您正在编写自己的模型,只需在flags()方法的返回值中包含Qt.ItemIsUserCheckable标志,并确保从data()方法返回Qt.CheckStateRole的有效值。
If you use the QStandardItemModel
class, include the Qt.ItemIsUserCheckable
flag in those you pass to each item's setFlags()
method, and set the check state for the Qt.CheckStateRole
with its setData()
method.
如果使用Qt.ItemIsUserCheckable类,则在传递给每个项目的setFlags()方法的标记中包含这个qt . standardtemmodel类,并使用它的setData()方法设置Qt.CheckStateRole的检查状态。
In an interactive Python session, type the following:
在交互式Python会话中,输入以下内容:
from PyQt4.QtGui import *
model = QStandardItemModel()
item = QStandardItem("Item")
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setData(QVariant(Qt.Checked), Qt.CheckStateRole)
model.appendRow(item)
view = QListView()
view.setModel(model)
view.show()
#2
21
I ended up using the method provided by David Boddie in the PyQt mailing list. Here's a working snippet based on his code:
最后我使用了PyQt邮件列表中David Boddie提供的方法。下面是一个基于他的代码的工作代码片段:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from random import randint
app = QApplication(sys.argv)
model = QStandardItemModel()
for n in range(10):
item = QStandardItem('Item %s' % randint(1, 100))
check = Qt.Checked if randint(0, 1) == 1 else Qt.Unchecked
item.setCheckState(check)
item.setCheckable(True)
model.appendRow(item)
view = QListView()
view.setModel(model)
view.show()
app.exec_()
Note: changed the call of setData
with a check role to setCheckState
and used setCheckable
instead of flags.
注意:使用检查角色将setData的调用更改为setCheckState,并使用setCheckable代替标志。
#1
10
If you are writing your own model, just include the Qt.ItemIsUserCheckable
flag in the return value from the flags()
method, and ensure that you return a valid value for the Qt.CheckStateRole
from the data()
method.
如果您正在编写自己的模型,只需在flags()方法的返回值中包含Qt.ItemIsUserCheckable标志,并确保从data()方法返回Qt.CheckStateRole的有效值。
If you use the QStandardItemModel
class, include the Qt.ItemIsUserCheckable
flag in those you pass to each item's setFlags()
method, and set the check state for the Qt.CheckStateRole
with its setData()
method.
如果使用Qt.ItemIsUserCheckable类,则在传递给每个项目的setFlags()方法的标记中包含这个qt . standardtemmodel类,并使用它的setData()方法设置Qt.CheckStateRole的检查状态。
In an interactive Python session, type the following:
在交互式Python会话中,输入以下内容:
from PyQt4.QtGui import *
model = QStandardItemModel()
item = QStandardItem("Item")
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setData(QVariant(Qt.Checked), Qt.CheckStateRole)
model.appendRow(item)
view = QListView()
view.setModel(model)
view.show()
#2
21
I ended up using the method provided by David Boddie in the PyQt mailing list. Here's a working snippet based on his code:
最后我使用了PyQt邮件列表中David Boddie提供的方法。下面是一个基于他的代码的工作代码片段:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from random import randint
app = QApplication(sys.argv)
model = QStandardItemModel()
for n in range(10):
item = QStandardItem('Item %s' % randint(1, 100))
check = Qt.Checked if randint(0, 1) == 1 else Qt.Unchecked
item.setCheckState(check)
item.setCheckable(True)
model.appendRow(item)
view = QListView()
view.setModel(model)
view.show()
app.exec_()
Note: changed the call of setData
with a check role to setCheckState
and used setCheckable
instead of flags.
注意:使用检查角色将setData的调用更改为setCheckState,并使用setCheckable代替标志。