[Qt-interest] boolean field checkbox QTableView
Linos
info at linos.es
Wed Feb 11 20:31:35 CET 2009
Andreas Pakulat escribió:
> On 11.02.09 15:19:18, Linos wrote:
>> Andreas Pakulat escribió:
>>> Then data() still returns something meaningful for the DisplayRole which
>>> you should be able to disable as well. That should also get rid of the
>>> combobox.
>> I have changed the data method to return a null QVariant in DisplayRole and i
>> know don't see the text in the column in display but without delegate i still
>> see the combobox to edit it with "True, False" values, i think this is the
>> default behavior to EditRole in boolean columns, i think the only solution here
>> it is the persistent editor that Christopher has pointed me.
>
> Attached is a small sample that works as you describe I believe, it allows
> to check/uncheck the checkbox in the checkbox column by clicking on it and
> it allows to edit the string just as normal by double clicking.
>
> As you can see one also needs to handle the CheckStateRole in setData, in
> this example I'm explicitly handling Qt::Checked/Qt::Unchecked, but it may
> also work with plain boolean.
>
> Andreas
>
>
I liked the idea of using a delegate for avoid subclass the model every time i
have a column with boolean values and because i was to use the same model
instance for one QDataWidgetMapper and one QTableView but using QSortProxyModel
and reimplementing setData too it is not needed, many thanks for the working
example Andreas, i thought i could not do the same using
QSqlRelationalTableModel withouth execute directly SQL that i was trying to
avoid but i have done calling the base implementation with Qt.EditRole. I paste
the code here to anyone with this problem in the future, is is python but easily
interpretable.
class GenericSortProxyModel(QSortFilterProxyModel):
def __init__(self, parent):
super(GenericSortProxyModel, self).__init__()
self.booleanSet = set()
self.nullVariant = QVariant()
def setParametros(self, booleanColumns=None):
if booleanColumns:
for columna in booleanColumns:
self.booleanSet.add(columna)
def flags(self, index):
if not index.isValid():
return Qt.ItemIsEnabled
if index.column() in self.booleanSet:
return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled
else:
return QSortFilterProxyModel.flags(self, index)
def data(self, index, role):
if not index.isValid():
return self.nullVariant()
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 self.nullVariant
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)
at the moment only support the boolean columns in a set but i will use this
model anytime a delegate can not do what i want easily. Thanks Andreas and
Christopher for their help.
More information about the Qt-interest-old
mailing list