That looks promising and would work -- if the QSqlQueryModel::data() would actually return data for the Qt::CheckStateRole.
As it turns out, QSqlQueryModel returns an invalid QVariant, if the provided role is not DisplayRole or EditRole.
So, I just call data() from QSqlQueryModel, but when I need the CheckStateRole, I call data() again with DisplayRole, but with a modified index to look at another column where the checkstate actually is. That way, I can put the "name" column at the very left and give it the checkbox, while having the "use" column at the very right. There, I can hide it by returning the base class' columnCount() minus 1 (or use hideSection() from the headerView).
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
row = index.row()
col = index.column()
data = super(SkyObjectModel, self).data(index, role)
if role == Qt.CheckStateRole and col == 0:
idx = self.createIndex(row, 8)
use,_ = super(SkyObjectModel, self).data(idx, Qt.DisplayRole).toInt()
return Qt.Checked if use == 1 else Qt.Unchecked
else:
return data
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return QVariant()
row = index.row()
col = index.column()
data = super(SkyObjectModel, self).data(index, role)
if role == Qt.CheckStateRole and col == 0:
idx = self.createIndex(row, 8)
use,_ = super(SkyObjectModel, self).data(idx, Qt.DisplayRole).toInt()
return Qt.Checked if use == 1 else Qt.Unchecked
else:
return data
To copy to clipboard, switch view to plain text mode
The result is what I had in mind: checkstate, but not "use" column:
Screen0023.jpg
With that problem out of the way, setting new checkstates should not be too difficult...
Thanks for your help, anda.
Bookmarks