Difference between revisions of "Filter Columns in a QFileSystemModel"

From Qt Wiki
Jump to: navigation, search
(Convert ExpressionEngine links)
(convert {{doclinkanchor}} to the improved {{doclink}})
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
 
 
 
[[Category:HowTo]]
 
[[Category:HowTo]]
[[Category:snippets]]
+
== Introduction ==
 +
Sometimes one does not want to display all the columns of a {{DocLink|QFileSystemModel}}.
  
= 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 {{DocLink|QHeaderView}}, but the alternative to subclass {{DocLink|QSortFilterProxyModel}} and reimplement the {{DocLink|QSortFilterProxyModel|filterAcceptsColumn}}() method also works well.
 
 
Sometimes one does not want to display all the columns of a [http://doc.qt.nokia.com/stable/qfilesystemmodel.html QFileSystemModel], like discussed in [http://developer.qt.nokia.com/forums/viewthread/3114/ this forum thread].
 
 
 
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] and reimplement the <code>filterAcceptsColumn()</code> method also works well.
 
  
 
== Column hiding using QHeaderView ==
 
== Column hiding using QHeaderView ==
  
All multi-column views provide access to a [http://doc.qt.nokia.com/stable/qheaderview.html QHeaderView] . 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.
+
All multi-column views provide access to a {{DocLink|QHeaderView}}. 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>
 
<code>
//hide the third and fourth column, note that the column numbers are 0-based
+
// hide the third and fourth column, note that the column numbers are 0-based
 
QHeaderView* hHeader = m_theTableview->horizontalHeader();
 
QHeaderView* hHeader = m_theTableview->horizontalHeader();
 
hHeader->hideSection(2);
 
hHeader->hideSection(2);
Line 27: Line 22:
 
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'''
 +
 
 
<code>
 
<code>
#include <QSortFilterProxyModel> ====
+
#include <QSortFilterProxyModel>
  
 
class FSMProxy : public QSortFilterProxyModel
 
class FSMProxy : public QSortFilterProxyModel
Line 35: Line 31:
 
  Q_OBJECT
 
  Q_OBJECT
  
public:
+
public:
FSMProxy(QObject *parent);
+
  FSMProxy(QObject *parent);
  
protected:
+
protected:
bool filterAcceptsColumn(int source_column, const QModelIndex &amp;source_parent) const;
+
  bool filterAcceptsColumn(int source_column, const QModelIndex& source_parent) const;
 
};
 
};
 
</code>
 
</code>
  
==== fsmproxy.cpp
+
'''fsmproxy.cpp'''
 +
 
 
<code>
 
<code>
FSMProxy::FSMProxy(QObject *parent)
+
FSMProxy::FSMProxy(QObject *parent) : QSortFilterProxyModel(parent) {}
: QSortFilterProxyModel(parent)
 
{
 
} ====
 
  
bool FSMProxy::filterAcceptsColumn(int source_column, const QModelIndex &amp;source_parent) const
+
bool FSMProxy::filterAcceptsColumn(int source_column, const QModelIndex& source_parent) const
 
{
 
{
 
  Q_UNUSED(source_parent)
 
  Q_UNUSED(source_parent)
  
    // adjust the columns you want to filter out here
+
// adjust the columns you want to filter out here
    // return false for those that will be hidden
+
// return false for those that will be hidden
  if(source_column 2 || source_column 3)
+
  if ( source_column == 2 || source_column == 3 ) return false;
return false;
+
 
 
  return true;
 
  return true;
 
}
 
}
 
</code>
 
</code>
  
==== Usage
+
'''Usage'''
 +
 
 
<code>
 
<code>
    QFileSystemModel *fsm = new QFileSystemModel(this);
+
QFileSystemModel *fsm = new QFileSystemModel(this);
    FSMProxy *proxy = new FSMProxy(this);
+
FSMProxy *proxy = new FSMProxy(this);
    proxy->setSourceModel(fsm);
+
proxy->setSourceModel(fsm);
    treeView->setModel(proxy);
+
treeView->setModel(proxy);
</code> ====
+
</code>

Latest revision as of 09:29, 23 October 2015

Introduction

Sometimes one does not want to display all the columns of 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 QHeaderView, but the alternative to subclass QSortFilterProxyModel and reimplement the filterAcceptsColumn() method also works well.

Column hiding using QHeaderView

All multi-column views provide access to a QHeaderView. 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.

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

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

  1. include <QSortFilterProxyModel>

class FSMProxy : public QSortFilterProxyModel {

Q_OBJECT
public:
 FSMProxy(QObject *parent);
protected:
 bool filterAcceptsColumn(int source_column, const QModelIndex& source_parent) const;

};

fsmproxy.cpp

FSMProxy::FSMProxy(QObject *parent) : QSortFilterProxyModel(parent) {}

bool FSMProxy::filterAcceptsColumn(int source_column, const QModelIndex& source_parent) const {

Q_UNUSED(source_parent)
// adjust the columns you want to filter out here
// return false for those that will be hidden
if ( source_column == 2 || source_column == 3 ) return false;
return true;

}

Usage

QFileSystemModel *fsm = new QFileSystemModel(this); FSMProxy *proxy = new FSMProxy(this); proxy->setSourceModel(fsm); treeView->setModel(proxy);