QSortFilterProxyModel subclass to add a checkbox

From Qt Wiki
Revision as of 09:27, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search


[toc align_right="yes" depth="3"]

CheckableProxyModel

The <code&gt;CheckableProxyModel&lt;/code&gt; class is a <code&gt;QSortFilterProxyModel&lt;/code&gt; class that decorates any <code&gt;QAbstractItemModel&lt;/code&gt; 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 <code&gt;QFileSystemModel&lt;/code&gt;.

Comparison

The <code&gt;CheckableSortFilterProxyModel&lt;/code&gt; described in this snippet also provides check boxes for <code&gt;QAbstractItemModel&lt;/code&gt; instances. However, there are differences in the way the classes work, making them suitable to different situations:
* <code&gt;CheckableSortFilterProxyModel&lt;/code&gt; translates a boolean value of the source model to a checkbox, while <code&gt;CheckableProxyModel&lt;/code&gt; stores this state externally from the source model, in the proxy model itself.
' <code&gt;CheckableSortFilterProxyModel&lt;/code&gt; is thus suitable for editing a model itself (like perhaps a <code&gt;QSqlTableModel&lt;/code&gt;), <code&gt;CheckableProxyModel&lt;/code&gt; can not do this.
'
<code&gt;CheckableSortFilterProxyModel&lt;/code&gt; can therefore not' be used if there is no boolean field in the source model, like in the case of a <code&gt;QFileSystemModel&lt;/code&gt;
'
<code&gt;CheckableSortFilterProxyModel&lt;/code&gt; 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. <code&gt;CheckableProxyModel&lt;/code&gt; adds a checkbox to the existing data (overriding an existing checkbox, if that is there).
* <code&gt;CheckableProxyModel&lt;/code&gt; works well on trees, automatically using the <code&gt;Qt::PartiallyChecked&lt;/code&gt; check state for branch nodes that have both checked and unchecked children. <code&gt;CheckableSortFilterProxyModel&lt;/code&gt; only edits the row itself, and does not support the <code&gt;PartiallyChecked&lt;/code&gt; state.
* <code&gt;CheckableProxyModel&lt;/code&gt; can be used as a kind of extended selection mechanism, while <code&gt;CheckableSortFilterProxyModel&lt;/code&gt; is suited to ease the editing of boolean values in the underlying model.
* <code&gt;CheckableSortFilterProxyModel&lt;/code&gt; supports making columns read only and setting columns as password columns.
* <code&gt;CheckableSortFilterProxyModel&lt;/code&gt; supports putting a checkbox in any column, while <code&gt;CheckableProxyModel&lt;/code&gt; only supports adding a checkbox to column 0.

You can combine both proxy models.

Usage

<code&gt;CheckableProxyModel&lt;/code&gt; can be used like any other proxy model.

<br /> QFileSystemModel* fsModel = new QFileSystemModel(this);

m_checkProxy = new CheckableProxyModel(this);<br /> m_checkProxy-&gt;setSourceModel(fsModel);<br /> ui-&gt;filteredTreeView-&gt;setModel(m_checkProxy);

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

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

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

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 <code&gt;CheckableProxyModel&lt;/code&gt; and the demo project are available from "this Gitorious project&quot;:https://gitorious.org/checkableproxymodel . Feedback on the class is very welcome in "this forum topic&quot;:http://developer.qt.nokia.com/forums/viewthread/5544 .