Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_gui_itemviews_qidentityproxymodel.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Stephen Kelly <stephen.kelly@kdab.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QIdentityProxyModel>
5#include <QDateTime>
6#include <QMap>
7#include <QModelIndex>
8#include <QVariant>
9
11{
12public:
13 enum Roles {
15 };
16};
17
18//! [0]
20{
21 // ...
22
23 void setDateFormatString(const QString &formatString)
24 {
25 m_formatString = formatString;
26 }
27
28 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
29 {
30 if (role != Qt::DisplayRole)
31 return QIdentityProxyModel::data(index, role);
32
33 const QModelIndex sourceIndex = mapToSource(index);
34 const QDateTime dateTime = sourceModel()->data(sourceIndex, SourceClass::DateRole).toDateTime();
35 return dateTime.toString(m_formatString);
36 }
37
38 QMap<int, QVariant> itemData(const QModelIndex &proxyIndex) const override
39 {
40 QMap<int, QVariant> map = QIdentityProxyModel::itemData(proxyIndex);
41 map[Qt::DisplayRole] = data(proxyIndex);
42 return map;
43 }
44
45private:
46 QString m_formatString;
47};
48//! [0]
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns the data stored under the given role for the item referred to by the index.
QMap< int, QVariant > itemData(const QModelIndex &proxyIndex) const override
Returns a map with values for all predefined roles in the model for the item at the given index.