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.
|
The QGroupingProxyModel class implements a grouping proxy model. More...
#include <qgroupingproxymodel.h>
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 |
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:
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:
At this point no grouping is applied and the source model items are displayed in the tree view as children of the group "Ungrouped".
To enable grouping it is required to call setModelColumn() to determine the source model colum the proxy shall operate on.
Once the model column is set, groups can be added by calling addGroup().
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.
Sorting can be achieved by cascading proxy models. The first proxy will group the model items and the second sorts them.
Group definitions can be saved and restored calling saveGroups() or restoreGroups().
|
explicit |
Constructs a QGroupingProxyModel with the given parent
.
QGroupingProxyModel::~QGroupingProxyModel | ( | ) |
Destroys the QGroupingProxyModel object.
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
.
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.
QModelIndex QGroupingProxyModel::mapToSource | ( | const QModelIndex & | proxyIndex | ) | const |
Returns the model index in the source model that corresponds to the proxyIndex
in the proxy model.
int QGroupingProxyModel::modelColumn | ( | ) | const |
Returns the source model column used for grouping items.
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:
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
.
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:
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.
void QGroupingProxyModel::setModelColumn | ( | int | column | ) |
Sets the source model column
that is used for grouping items.
|
virtual |
Sets the given sourceModel
to be processed by the proxy model.
|
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.
|
readwrite |
Returns true if the groups are spanned over all columns. Otherwise false.
|
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.