QAdvanced Item View  0.4
The QAIV (Qt Advanced Item Views) library provides GUI and utility classes extending the capabilities of the Qt item views.
 All Classes Functions Variables Enumerations Enumerator Properties Groups Pages
Public Member Functions | Properties | List of all members
QGroupingProxyModel Class Reference

The QGroupingProxyModel class implements a grouping proxy model. More...

#include <qgroupingproxymodel.h>

Inheritance diagram for QGroupingProxyModel:

Public Member Functions

 QGroupingProxyModel (QObject *parent=0)
 
 ~QGroupingProxyModel ()
 
void addGroup (const QString &text, const QVariant &value=QVariant())
 
void addGroup (const QIcon &icon, const QString &text, const QVariant &value=QVariant())
 
int columnCount (const QModelIndex &parent=QModelIndex()) const
 
QVariant data (const QModelIndex &proxyIndex, int role) const
 
Qt::ItemFlags flags (const QModelIndex &index) const
 
bool groupsSpanned () const
 
QVariant headerData (int section, Qt::Orientation orientation, int role) const
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const
 
int findText (const QString &text) const
 
QModelIndex mapFromSource (const QModelIndex &sourceIndex) const
 
QModelIndex mapToSource (const QModelIndex &proxyIndex) const
 
int modelColumn () const
 
QModelIndex parent (const QModelIndex &child) const
 
bool removeGroup (int index)
 
bool restoreGroups (const QByteArray &data)
 
int rowCount (const QModelIndex &parent=QModelIndex()) const
 
QByteArray saveGroups () const
 
bool setData (const QModelIndex &index, const QVariant &value, int role)
 
void setGroupSectionHeader (const QString &header)
 
void setGroupsSpanned (bool on)
 
void setModelColumn (int column)
 
virtual void setSourceModel (QAbstractItemModel *sourceModel)
 
void setUngroupedItemTitle (const QString &title)
 
void setUngroupedItemTitle (const QString &title, const QIcon &icon)
 
virtual QSize span (const QModelIndex &index) const
 

Properties

bool groupsSpanned
 
int modelColumn
 

Detailed Description

The QGroupingProxyModel class implements a grouping proxy model.

The QGroupingProxyModel can be used for grouping items. A tree structure is created to arrange the source items as defined by the group definitions.

Let's assume that we want to group the items provided by a custom model. The code to set up the model and the view, without grouping, would look like this:

QTreeView* treeView = new QTreeView;
QStandardItemModel* model = new QStandardItemModel(this);
treeView->setModel(model);

To add grouping support to mDataModel, we need to create a QGroupingProxyModel, call setSourceModel() with the mDataModel as argument, and install the QGroupingProxyModel on the view:

QTreeView* treeView = new QTreeView;
QStandardItemModel* model = new QStandardItemModel(this);
proxy->setSourceModel(model);
treeiew->setModel(proxy);

At this point no grouping is applied and the source model items are displayed in the tree view as children of the group "Ungrouped".

qgroupingmodel01.png

Grouping

To enable grouping it is required to call setModelColumn() to determine the source model colum the proxy shall operate on.

proxy->setModelColumn(2);

Once the model column is set, groups can be added by calling addGroup().

QTreeView* treeView = new QTreeView;
QStandardItemModel* model = new QStandardItemModel(this);
proxy->setModelColumn(2);
proxy->addGroup(QIcon(":/icons/folder"), "Qt's Tools", "Qt's tools");
proxy->setSourceModel(mItemModel);
treeView->setModel(mGroupingProxy);

A second group 'Qt's Tools' is added to the tree listing all source items matching the value 'Qt's tools' in the selected model column.

Note
The addGroup() call use different values for displaying the group title and matching.
qgroupingmodel02.png

Sorting

Sorting can be achieved by cascading proxy models. The first proxy will group the model items and the second sorts them.

QTreeView* treeView = new QTreeView;
QStandardItemModel* model = new QStandardItemModel(this);
QGroupingProxyModel* goupingProxy = new QGroupingProxyModel(this);
goupingProxy->setModelColumn(2);
goupingProxy->addGroup(QIcon(":/icons/folder"), "Qt's Tools", "Qt's tools");
proxy->setSourceModel(model);
QSortFilterProxyModel* sortFilterProxy = new QSortFilterProxyModel(this);
sortFilterProxy->setSourceModel(goupingProxy);
treeView->setModel(sortFilterProxy);

Save and Restore Groups

Group definitions can be saved and restored calling saveGroups() or restoreGroups().

Constructor & Destructor Documentation

QGroupingProxyModel::QGroupingProxyModel ( QObject *  parent = 0)
explicit

Constructs a QGroupingProxyModel with the given parent.

QGroupingProxyModel::~QGroupingProxyModel ( )

Destroys the QGroupingProxyModel object.

Member Function Documentation

void QGroupingProxyModel::addGroup ( const QString &  text,
const QVariant &  value = QVariant() 
)

Creates a new group named text matching the specified value. This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

void QGroupingProxyModel::addGroup ( const QIcon &  icon,
const QString &  text,
const QVariant &  value = QVariant() 
)

Creates a new group named text, with an icon and matching the specified value. This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

int QGroupingProxyModel::columnCount ( const QModelIndex &  parent = QModelIndex()) const

Returns the number of columns for the children of the given parent.

See also
rowCount()
QVariant QGroupingProxyModel::data ( const QModelIndex &  proxyIndex,
int  role 
) const

QAbstractItemModel::data()

Qt::ItemFlags QGroupingProxyModel::flags ( const QModelIndex &  index) const

QAbstractItemModel::data()

QVariant QGroupingProxyModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role 
) const

QAbstractItemModel::data()

QModelIndex QGroupingProxyModel::index ( int  row,
int  column,
const QModelIndex &  parent = QModelIndex() 
) const

QAbstractItemModel::data()

QModelIndex QGroupingProxyModel::mapFromSource ( const QModelIndex &  sourceIndex) const

Returns the model index in the proxy model that corresponds to the sourceIndex from the source model.

See also
mapToSource()
QModelIndex QGroupingProxyModel::mapToSource ( const QModelIndex &  proxyIndex) const

Returns the model index in the source model that corresponds to the proxyIndex in the proxy model.

See also
mapToSource()
int QGroupingProxyModel::modelColumn ( ) const

Returns the source model column used for grouping items.

See also
setModelColumn()
QModelIndex QGroupingProxyModel::parent ( const QModelIndex &  child) const

Returns the parent of the model item with the given index. If the item has no parent, an invalid QModelIndex is returned.

bool QGroupingProxyModel::removeGroup ( int  index)

Removes the group specified by the given index.

bool QGroupingProxyModel::restoreGroups ( const QByteArray &  data)

Restores the group definitions. Returns true if the groups are restored; otherwise returns false. Typically this is used in conjunction with QSettings to restore the group definitions from a past session. Here is an example:

QSettings settings;
groupingProxyModel->restoreGroups(settings.value("groups").toByteArray());

A failure to restore the group definitions may result from either invalid or out-of-date data in the supplied byte array.

int QGroupingProxyModel::rowCount ( const QModelIndex &  parent = QModelIndex()) const

Returns the number of rows under the given parent. When the parent is valid it means that rowCount is returning the number of children of parent.

See also
columnCount()
QByteArray QGroupingProxyModel::saveGroups ( ) const

Saves the proxy's group definitions. Typically this is used in conjunction with QSettings to remember the groups for a future session. A version number is stored as part of the data. Here is an example:

QSettings settings;
settings.setValue("groups", groupingProxyModel->saveGroups());
See also
restoreGroups()
bool QGroupingProxyModel::setData ( const QModelIndex &  index,
const QVariant &  value,
int  role 
)

Sets the role data for the item at index to value.

void QGroupingProxyModel::setGroupsSpanned ( bool  on)

If on is true, the groups are spanned over all columns.

See also
span()
void QGroupingProxyModel::setModelColumn ( int  column)

Sets the source model column that is used for grouping items.

See also
modelColumn()
void QGroupingProxyModel::setSourceModel ( QAbstractItemModel *  sourceModel)
virtual

Sets the given sourceModel to be processed by the proxy model.

QSize QGroupingProxyModel::span ( const QModelIndex &  index) const
virtual

Returns the row and column span of the item represented by index. If groupsSpanned is enabled QSize(columnCount(), 1) is returned for all root indexes.

Property Documentation

bool QGroupingProxyModel::groupsSpanned
readwrite

Returns true if the groups are spanned over all columns. Otherwise false.

See also
span()
int QGroupingProxyModel::modelColumn
readwrite

This property holds the column in the source model that is used for grouping items.

By default, this property contains 0, indicating that the first column in the source model will be used.

See also
modelColumn() setModelColumn()

The documentation for this class was generated from the following files: