QSortFilterProxyModel subclass to add a checkbox: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Formatting)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Snippets]]<br />[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
 
[[Category:Snippets]]
 


= CheckableProxyModel =
= CheckableProxyModel =


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 '''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 ==
== Comparison ==


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.
The '''CheckableSortFilterProxyModel''' described in [[QSortFilterProxyModel_subclass_for_readonly_columns_columns_with_checkboxes_and_password_columns|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.
You can combine both proxy models.
Line 13: Line 22:
== Usage ==
== Usage ==


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


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


m_checkProxy = new CheckableProxyModel(this);<br /> m_checkProxy-&gt;setSourceModel(fsModel);<br /> ui-&gt;filteredTreeView-&gt;setModel(m_checkProxy);
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<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 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<br /> connect(ui-&gt;cmdReset, SIGNAL (clicked()), m_checkProxy, SLOT (resetToDefault()));
//connect a reset button to reset the checkboxes
connect(ui->cmdReset, SIGNAL (clicked()), m_checkProxy, SLOT (resetToDefault()));


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


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


[[Image:http://dl.dropbox.com/u/16442531/CheckableProxyModelDemo.png|Screenshot CheckableProxyModel demo]]
http://dl.dropbox.com/u/16442531/CheckableProxyModelDemo.png


== Code ==
== Code ==


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 .
The code for the'''CheckableProxyModel''' and the demo project are available from [https://gitorious.org/checkableproxymodel this Gitorious project] . Feedback on the class is very welcome in [http://developer.qt.nokia.com/forums/viewthread/5544 this forum topic] .

Latest revision as of 06:55, 1 April 2015

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 .