[Qt-interest] boolean field checkbox QTableView

Linos info at linos.es
Wed Feb 11 13:17:11 CET 2009


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.
> 
> Andreas
> 

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 
column and to change the value i have to click in "True" text and it gives me a 
combobox with the values "True, False", i would prefer to use directly the 
checkbox not only to see the value but to change it without "True, False" text 
and without a combobox.



More information about the Qt-interest-old mailing list