Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should create a model that support Qt.CheckStateRole in data and SetData methods and the flag Qt.ItemIsUserCheckable in the flags method.</p> <p>I Paste you here an example i am using in a project, this is a QSortFilterProxyModel generic implementation to use in any model but you can use the same ideas in your model implementation, obviously i am using internal structures in this subclass you have not directly in PyQt and are attached to my internal implementation (self.booleanSet and self.readOnlySet).</p> <pre><code>def flags(self, index): if not index.isValid(): return Qt.ItemIsEnabled if index.column() in self.booleanSet: return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled elif index.column() in self.readOnlySet: return Qt.ItemIsSelectable | Qt.ItemIsEnabled else: return QSortFilterProxyModel.flags(self, index) def data(self, index, role): if not index.isValid(): return QVariant() if index.column() in self.booleanSet and role in (Qt.CheckStateRole, Qt.DisplayRole): if role == Qt.CheckStateRole: value = QVariant(Qt.Checked) if index.data(Qt.EditRole).toBool() else QVariant(Qt.Unchecked) return value else: #if role == Qt.DisplayRole: return QVariant() else: return QSortFilterProxyModel.data(self, index, role) def setData(self, index, data, role): if not index.isValid(): return False if index.column() in self.booleanSet and role == Qt.CheckStateRole: value = QVariant(True) if data.toInt()[0] == Qt.Checked else QVariant(False) return QSortFilterProxyModel.setData(self, index, value, Qt.EditRole) else: return QSortFilterProxyModel.setData(self, index, data, role) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload