QSortFilterProxyModel subclass to add a checkbox
From Qt Wiki
Revision as of 16:38, 14 January 2015 by Maintenance script
class is a
class that decorates any
with a checkbox in column 0 of every row. The model is especially suited for use on tree-type models, but will work just as well on tables or lists. The model uses an efficient way to internally store the check state of every item, making the model usable with large and/or lazily populated models like
described in this snippet also provides check boxes for
instances. However, there are differences in the way the classes work, making them suitable to different situations:
- translates a boolean value of the source model to a checkbox, while
CheckableSortFilterProxyModelstores this state externally from the source model, in the proxy model itself.
- is thus suitable for editing a model itself (like perhaps a
QSqlTableModelcan not do this.
- can therefore not be used if there is no boolean field in the source model, like in the case of a
- can not make columns that contain a check box as well as some other piece of data. A column can only contain a checkbox, but no other data like a name.
CheckableSortFilterProxyModeladds a checkbox to the existing data (overriding an existing checkbox, if that is there).
- works well on trees, automatically using the
CheckableProxyModelcheck state for branch nodes that have both checked and unchecked children.
Qt::PartiallyCheckedonly edits the row itself, and does not support the
- can be used as a kind of extended selection mechanism, while
CheckableProxyModelis suited to ease the editing of boolean values in the underlying model.
- supports making columns read only and setting columns as password columns.
- supports putting a checkbox in any column, while
CheckableSortFilterProxyModelonly supports adding a checkbox to column 0.
You can combine both proxy models.
can be used like any other proxy model.
The code above is the main code that sets up the application you see on the screen shot below:
CodeThe code for the
and the demo project are available from this Gitorious project [gitorious.org] . Feedback on the class is very welcome in this forum topic [developer.qt.nokia.com] .