QSortFilterProxyModel subclass to add a checkbox

From Qt Wiki
Revision as of 16:38, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

CheckableProxyModel

The

CheckableProxyModel

class is a

QSortFilterProxyModel

class that decorates any

QAbstractItemModel

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

QFileSystemModel

.

Comparison

The

CheckableSortFilterProxyModel

described in this snippet also provides check boxes for

QAbstractItemModel

instances. However, there are differences in the way the classes work, making them suitable to different situations:

  • CheckableSortFilterProxyModel
    
    translates a boolean value of the source model to a checkbox, while
    CheckableProxyModel
    
    stores this state externally from the source model, in the proxy model itself.
    • CheckableSortFilterProxyModel
      
      is thus suitable for editing a model itself (like perhaps a
      QSqlTableModel
      
      ),
      CheckableProxyModel
      
      can not do this.
    • CheckableSortFilterProxyModel
      
      can therefore not be used if there is no boolean field in the source model, like in the case of a
      QFileSystemModel
      
    • CheckableSortFilterProxyModel
      
      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.
      CheckableProxyModel
      
      adds a checkbox to the existing data (overriding an existing checkbox, if that is there).
  • CheckableProxyModel
    
    works well on trees, automatically using the
    Qt::PartiallyChecked
    
    check state for branch nodes that have both checked and unchecked children.
    CheckableSortFilterProxyModel
    
    only edits the row itself, and does not support the
    PartiallyChecked
    
    state.
  • CheckableProxyModel
    
    can be used as a kind of extended selection mechanism, while
    CheckableSortFilterProxyModel
    
    is suited to ease the editing of boolean values in the underlying model.
  • CheckableSortFilterProxyModel
    
    supports making columns read only and setting columns as password columns.
  • CheckableSortFilterProxyModel
    
    supports putting a checkbox in any column, while
    CheckableProxyModel
    
    only supports adding a checkbox to column 0.

You can combine both proxy models.

Usage

CheckableProxyModel

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:

Screenshot CheckableProxyModel demo

Code

The code for the

CheckableProxyModel

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

Categories: