QSortFilterProxyModel subclass to add a checkbox: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=CheckableProxyModel=
[[Category:Snippets]]<br />[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]


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


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


The <code>CheckableSortFilterProxyModel</code> described in [[QSortFilterProxyModel subclass for readonly columns columns with checkboxes and password columns|this snippet]] also provides check boxes for <code>QAbstractItemModel</code> instances. However, there are differences in the way the classes work, making them suitable to different situations:
== Comparison ==


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


You can combine both proxy models.
You can combine both proxy models.


==Usage==
== Usage ==


<code>CheckableProxyModel</code> can be used like any other proxy model.
&lt;code&amp;gt;CheckableProxyModel&amp;lt;/code&amp;gt; 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:
<code><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()));


[[Image:CheckableProxyModelDemo.png|Screenshot CheckableProxyModel demo]]
//do something when the checked boxes changed<br /> connect(m_checkProxy, SIGNAL (checkedNodesChanged()), this, SLOT (selectedItemsChanged()));<br /></code>


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


The code for the <code>CheckableProxyModel</code> and the demo project are available from [https://gitorious.org/checkableproxymodel this Gitorious project] ''[gitorious.org]'' . Feedback on the class is very welcome in [http://developer.qt.nokia.com/forums/viewthread/5544 this forum topic] ''[developer.qt.nokia.com]'' .
[[Image:http://dl.dropbox.com/u/16442531/CheckableProxyModelDemo.png|Screenshot CheckableProxyModel demo]]


===Categories:===
== Code ==


* [[:Category:snippets|snippets]]
The code for the &lt;code&amp;gt;CheckableProxyModel&amp;lt;/code&amp;gt; and the demo project are available from &quot;this Gitorious project&amp;quot;:https://gitorious.org/checkableproxymodel . Feedback on the class is very welcome in &quot;this forum topic&amp;quot;:http://developer.qt.nokia.com/forums/viewthread/5544 .

Revision as of 09:27, 24 February 2015


[toc align_right="yes&quot; depth="3&quot;]

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 .