Filter Columns in a QFileSystemModel: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=How to Filter Columns in a QFileSystemModel=
[[Category:HowTo]]<br />[[Category:snippets]]


Sometimes one does not want to display all the columns of a [http://doc.qt.nokia.com/stable/qfilesystemmodel.html QFileSystemModel] ''[doc.qt.nokia.com]'', like discussed in [http://developer.qt.nokia.com/forums/viewthread/3114/ this forum thread] ''[developer.qt.nokia.com]''.
= How to Filter Columns in a QFileSystemModel =


This can be achieved in two ways, either by hiding the columns in the view, by using a specialized proxy model. The easiest way is to hide the columns using the <code>QHeaderView</code>, but the alternative to subclass [http://doc.qt.nokia.com/stable/qsortfilterproxymodel.html QSortFilterProxyModel] ''[doc.qt.nokia.com]'' and reimplement the <code>filterAcceptsColumn()</code> method also works well.
Sometimes one does not want to display all the columns of a &quot;QFileSystemModel&amp;quot;:http://doc.qt.nokia.com/stable/qfilesystemmodel.html, like discussed in &quot;this forum thread&amp;quot;:http://developer.qt.nokia.com/forums/viewthread/3114/.


==Column hiding using QHeaderView==
This can be achieved in two ways, either by hiding the columns in the view, by using a specialized proxy model. The easiest way is to hide the columns using the &lt;code&amp;gt;QHeaderView&amp;lt;/code&amp;gt;, but the alternative to subclass &quot;QSortFilterProxyModel&amp;quot;:http://doc.qt.nokia.com/stable/qsortfilterproxymodel.html and reimplement the &lt;code&amp;gt;filterAcceptsColumn()&lt;/code&amp;gt; method also works well.


All multi-column views provide access to a [http://doc.qt.nokia.com/stable/qheaderview.html QHeaderView] ''[doc.qt.nokia.com]'' . This is the widget that provides the headers above the columns of data, and allows the user to resize those columns, or click the headers to trigger a sorting order. You can also use this widget to hide columns in your view.
== Column hiding using QHeaderView ==
 
All multi-column views provide access to a &quot;QHeaderView&amp;quot;:http://doc.qt.nokia.com/stable/qheaderview.html . This is the widget that provides the headers above the columns of data, and allows the user to resize those columns, or click the headers to trigger a sorting order. You can also use this widget to hide columns in your view.
 
<code><br />//hide the third and fourth column, note that the column numbers are 0-based<br />QHeaderView* hHeader = m_theTableview-&gt;horizontalHeader();<br />hHeader-&gt;hideSection(2);<br />hHeader-&gt;hideSection(3);<br /></code>


The alternative method of using a custom proxy model is outlined below.
The alternative method of using a custom proxy model is outlined below.


==Column hiding using QSortFilterProxyModel==
== Column hiding using QSortFilterProxyModel ==


Be aware that the column count is 0-based and that it is hard coded.
Be aware that the column count is 0-based and that it is hard coded.


====fsmproxy.h====
==== fsmproxy.h<br /><code><br />#include &lt;QSortFilterProxyModel&amp;gt; ====
 
class FSMProxy : public QSortFilterProxyModel<br />{<br /> Q_OBJECT
 
public:<br /> FSMProxy(QObject *parent);
 
protected:<br /> bool filterAcceptsColumn(int source_column, const QModelIndex &amp;source_parent) const;<br />};<br /></code>


====fsmproxy.cpp====
==== fsmproxy.cpp<br /><code><br />FSMProxy::FSMProxy(QObject *parent)<br /> : QSortFilterProxyModel(parent)<br />{<br />} ====


====Usage====
bool FSMProxy::filterAcceptsColumn(int source_column, const QModelIndex &amp;source_parent) const<br />{<br /> Q_UNUSED(source_parent)


===Categories:===
    // adjust the columns you want to filter out here<br />    // return false for those that will be hidden<br /> if(source_column  2 || source_column  3)<br /> return false;<br /> return true;<br />}<br /></code>


* [[:Category:HowTo|HowTo]]
==== Usage<br /><code><br />    QFileSystemModel *fsm = new QFileSystemModel(this);<br />    FSMProxy *proxy = new FSMProxy(this);<br />    proxy-&gt;setSourceModel(fsm);<br />    treeView-&gt;setModel(proxy);<br /></code> ====
* [[:Category:snippets|snippets]]

Revision as of 09:54, 24 February 2015


How to Filter Columns in a QFileSystemModel

Sometimes one does not want to display all the columns of a "QFileSystemModel&quot;:http://doc.qt.nokia.com/stable/qfilesystemmodel.html, like discussed in "this forum thread&quot;:http://developer.qt.nokia.com/forums/viewthread/3114/.

This can be achieved in two ways, either by hiding the columns in the view, by using a specialized proxy model. The easiest way is to hide the columns using the <code&gt;QHeaderView&lt;/code&gt;, but the alternative to subclass "QSortFilterProxyModel&quot;:http://doc.qt.nokia.com/stable/qsortfilterproxymodel.html and reimplement the <code&gt;filterAcceptsColumn()</code&gt; method also works well.

Column hiding using QHeaderView

All multi-column views provide access to a "QHeaderView&quot;:http://doc.qt.nokia.com/stable/qheaderview.html . This is the widget that provides the headers above the columns of data, and allows the user to resize those columns, or click the headers to trigger a sorting order. You can also use this widget to hide columns in your view.

<br />//hide the third and fourth column, note that the column numbers are 0-based<br />QHeaderView* hHeader = m_theTableview-&gt;horizontalHeader();<br />hHeader-&gt;hideSection(2);<br />hHeader-&gt;hideSection(3);<br />

The alternative method of using a custom proxy model is outlined below.

Column hiding using QSortFilterProxyModel

Be aware that the column count is 0-based and that it is hard coded.

==== fsmproxy.h

<br />#include &lt;QSortFilterProxyModel&amp;gt; ====

class FSMProxy : public QSortFilterProxyModel<br />{<br /> Q_OBJECT

public:<br /> FSMProxy(QObject *parent);

protected:<br /> bool filterAcceptsColumn(int source_column, const QModelIndex &amp;source_parent) const;<br />};<br />

==== fsmproxy.cpp

<br />FSMProxy::FSMProxy(QObject *parent)<br /> : QSortFilterProxyModel(parent)<br />{<br />} ====

bool FSMProxy::filterAcceptsColumn(int source_column, const QModelIndex &amp;source_parent) const<br />{<br /> Q_UNUSED(source_parent)

    // adjust the columns you want to filter out here<br />    // return false for those that will be hidden<br /> if(source_column  2 || source_column  3)<br /> return false;<br /> return true;<br />}<br />

Usage
<br />    QFileSystemModel *fsm = new QFileSystemModel(this);<br />    FSMProxy *proxy = new FSMProxy(this);<br />    proxy-&gt;setSourceModel(fsm);<br />    treeView-&gt;setModel(proxy);<br />