[Qt-interest] boolean field checkbox QTableView
Linos
info at linos.es
Wed Feb 11 15:19:18 CET 2009
Andreas Pakulat escribió:
> On 11.02.09 13:17:11, Linos wrote:
>> Andreas Pakulat escribió:
>>> On 11.02.09 10:50:42, Christopher Rasch-Olsen Raa wrote:
>>>> Onsdag 11 februar 2009 09:55:46 skrev Linos:
>>>>> Hello,
>>>>> I have a QTableView with a QSqlRelationalTableModel and i want the boolean
>>>>> colums to be checkboxes in display and edit, I have searching the forum but
>>>>> i have not understand the full picture here i think, i have tried two ways
>>>>> to do that:
>>>>>
>>>>> -Create a QItemDelegate subclass and reimplement the methods createEditor,
>>>>> setEditorData, setModelData and updateEditorGeometry to represent a
>>>>> checkbox on edit.
>>>>>
>>>>> -Create a QSortProxyModel subclass and reimplement flags to add a
>>>>> Qt.ItemIsUserCheckable on the boolean column an check for
>>>>> Qt::CheckStateRole in data method to reply with the boolean value of the
>>>>> column.
>>>>>
>>>>> If i use the sortproxymodel i get the checkbox in display with the correct
>>>>> values but i still see the "true, false" strings and to change the value i
>>>>> get a combobox with the values true or false.
>>>>>
>>>>> If i use the delegate i get the checkbox to edit on doble click but i dont
>>>>> see checkboxes in display mode, only my well know "true, false" strings.
>>>>>
>>>>> If i use the delegate and the sortproxymodel i get a checkbox on the left
>>>>> of the cell on display mode and when i try to change the value i see other
>>>>> checkbox at the right of the first where i can change the value, after i
>>>>> commit the change the second checkbox is gone and the left display checkbox
>>>>> shows the change.
>>>>>
>>>>> Do i have any way to have a checkbox column in display and edit (the same)
>>>>> and not see the text "true, false"?
>>>> I recently looked for a way to do this myself. What I had to do was create a
>>>> QItemDelegate and use that on the checkable column. Next I subclassed
>>>> QSqlTableWidget and overloaded flags() so that returned the proper flags for
>>>> that column. Next I used QTableView::openPersistentEditor() on the fields in
>>>> the checkable column to make them always display. There are some clues to be
>>>> found in the demo-applications.
>>> I haven't use the sql-models/views yet myself but isn't it easier to
>>> subclass QSqlTableModel, overwrite flags() as needed there (returning
>>> Qt::UserCheckable for the right column) and then also data() to return a
>>> meaningful value for the Qt::CheckStateRole. Then put the model into a
>>> plain old QTableView and it just works. Without a custom item delegate or
>>> anything else. At least this approach works with custom models as well as
>>> QStandardItemModel.
>> I have tried to do that but without a delegate i see the checkbox correctly
>> checked or not but i see too the string value, anythink like "x True" in the
>
> 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.
>
> Andreas
>
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.
Miguel Angel.
More information about the Qt-interest-old
mailing list