[ACCEPTED]-A ListView of checkboxes in PyQt-qlistview
I ended up using the method provided by 4 David Boddie in the PyQt mailing list. Here's a working 3 snippet based on his code:
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 2 the call of setData
with a check role to setCheckState
and used 1 setCheckable
instead of flags.
If you are writing your own model, just 8 include the Qt.ItemIsUserCheckable
flag in the return value from 7 the flags()
method, and ensure that you return
a 6 valid value for the Qt.CheckStateRole
from the data()
method.
If 5 you use the QStandardItemModel
class, include the Qt.ItemIsUserCheckable
flag in 4 those you pass to each item's setFlags()
method, and 3 set the check
state for the Qt.CheckStateRole
with its setData()
method.
In 2 an interactive Python session, type the 1 following:
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()
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.