QSortFilterProxyModel subclass to add a checkbox

From Qt Wiki
Jump to: navigation, search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.


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.

 QFileSystemModel* fsModel = new QFileSystemModel(this);

m_checkProxy = new CheckableProxyModel(this);
 m_checkProxy->setSourceModel(fsModel);
 ui->filteredTreeView->setModel(m_checkProxy);

//connect a checkbox to control the default state of the checkboxes
 connect(ui->chkSetDefaultChecked, SIGNAL (toggled(bool)), m_checkProxy, SLOT (setDefaultCheckState(bool)));
 m_checkProxy->setDefaultCheckState(ui->chkSetDefaultChecked->isChecked());

//connect a reset button to reset the checkboxes
 connect(ui->cmdReset, SIGNAL (clicked()), m_checkProxy, SLOT (resetToDefault()));

//do something when the checked boxes changed
 connect(m_checkProxy, SIGNAL (checkedNodesChanged()), this, SLOT (selectedItemsChanged()));

The code above is the main code that sets up the application you see on the screen shot below:

CheckableProxyModelDemo.png

Code

The code for theCheckableProxyModel and the demo project are available from this Gitorious project . Feedback on the class is very welcome in this forum topic .