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.
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.
In an interactive Python session, type the 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()