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
qqmltablemodelcolumn.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtQml/qqmlinfo.h>
8
10
11/*!
12 \qmltype TableModelColumn
13//! \nativetype QQmlTableModelColumn
14 \inqmlmodule Qt.labs.qmlmodels
15 \brief Represents a column in a model.
16 \since 5.14
17
18 The TableModelColumn class represents columns in TableModel.
19 TableModel supports JavaScript/JSON data where each row is an object,
20 a list of simple key-value pairs where the keys are unordered.
21
22 \code
23 {
24 // Each property is one cell/column.
25 checked: false,
26 amount: 1,
27 fruitType: "Apple",
28 fruitName: "Granny Smith",
29 fruitPrice: 1.50
30 },
31 // ...
32 \endcode
33
34 However, models in Qt are manipulated via row and column indices. Specifying
35 the columns with TableModelColumn allows a mapping between Qt's built-in
36 roles to any property in each row object.
37
38 \snippet qml/tablemodel/fruit-example-simpledelegate.qml file
39
40 TableModelColumn also has basic read-only support for complex rows. For more
41 information, see \l {Supported Row Data Structures}.
42
43 \note Most of the above concepts also apply to TreeModel, except in
44 TreeModel each row represents a node of the tree.
45
46 \section1 Supported Roles
47
48 TableModelColumn supports all of \l {Qt::ItemDataRole}{Qt's roles},
49 with the exception of \c Qt::InitialSortOrderRole.
50 Roles can be accessed by as listed below, e.g.
51 \code
52 text: display
53
54 required property string display
55 \endcode
56
57 \table
58 \row \li Qt::DisplayRole \li display
59 \row \li Qt::DecorationRole \li decoration
60 \row \li Qt::EditRole \li edit
61 \row \li Qt::ToolTipRole \li toolTip
62 \row \li Qt::StatusTipRole \li statusTip
63 \row \li Qt::WhatsThisRole \li whatsThis
64 \row \li Qt::FontRole \li font
65 \row \li Qt::TextAlignmentRole \li textAlignment
66 \row \li Qt::BackgroundRole \li background
67 \row \li Qt::ForegroundRole \li foreground
68 \row \li Qt::CheckStateRole \li checkState
69 \row \li Qt::AccessibleTextRole \li accessibleText
70 \row \li Qt::AccessibleDescriptionRole \li accessibleDescription
71 \row \li Qt::SizeHintRole \li sizeHintRoleNam
72 \endtable
73
74 \sa TableModel, TableView
75*/
76
77static constexpr QLatin1StringView displayRoleName("display");
78static constexpr QLatin1StringView decorationRoleName("decoration");
79static constexpr QLatin1StringView editRoleName("edit");
80static constexpr QLatin1StringView toolTipRoleName("toolTip");
81static constexpr QLatin1StringView statusTipRoleName("statusTip");
82static constexpr QLatin1StringView whatsThisRoleName("whatsThis");
83
84static constexpr QLatin1StringView fontRoleName("font");
85static constexpr QLatin1StringView textAlignmentRoleName("textAlignment");
86static constexpr QLatin1StringView backgroundRoleName("background");
87static constexpr QLatin1StringView foregroundRoleName("foreground");
88static constexpr QLatin1StringView checkStateRoleName("checkState");
89
90static constexpr QLatin1StringView accessibleTextRoleName("accessibleText");
91static constexpr QLatin1StringView accessibleDescriptionRoleName("accessibleDescription");
92
93static constexpr QLatin1StringView sizeHintRoleName("sizeHint");
94
95
96QQmlTableModelColumn::QQmlTableModelColumn(QObject *parent)
97 : QObject(parent)
98{
99}
100
101QQmlTableModelColumn::~QQmlTableModelColumn()
102{
103}
104
105#define DEFINE_ROLE_PROPERTIES(getterGetterName, getterSetterName, getterSignal, roleName) QJSValue
106 QQmlTableModelColumn::getterGetterName() const \
107{
108 return mGetters.value(roleName); \
109}void
110
111 QQmlTableModelColumn::getterSetterName(const QJSValue &stringOrFunction) \
112{
113 if (!stringOrFunction.isString() && !stringOrFunction.isCallable()) {
114 qmlWarning(this).quote() << "getter for " << roleName << " must be a function";
115 return;
116 }
117 if (stringOrFunction.strictlyEquals(decoration()))
118 return;
119
120 mGetters[roleName] = stringOrFunction;
121 emit decorationChanged(); \
122}
123
124DEFINE_ROLE_PROPERTIES(display, setDisplay, displayChanged,
125 displayRoleName)
126DEFINE_ROLE_PROPERTIES(decoration, setDecoration, decorationChanged,
127 decorationRoleName)
128DEFINE_ROLE_PROPERTIES(edit, setEdit, editChanged,
129 editRoleName)
130DEFINE_ROLE_PROPERTIES(toolTip, setToolTip, toolTipChanged,
131 toolTipRoleName)
132DEFINE_ROLE_PROPERTIES(statusTip, setStatusTip, statusTipChanged,
133 statusTipRoleName)
134DEFINE_ROLE_PROPERTIES(whatsThis, setWhatsThis, whatsThisChanged,
135 whatsThisRoleName)
136
137DEFINE_ROLE_PROPERTIES(font, setFont, fontChanged,
138 fontRoleName)
139DEFINE_ROLE_PROPERTIES(textAlignment, setTextAlignment, textAlignmentChanged,
140 textAlignmentRoleName)
141DEFINE_ROLE_PROPERTIES(background, setBackground, backgroundChanged,
142 backgroundRoleName)
143DEFINE_ROLE_PROPERTIES(foreground, setForeground, foregroundChanged,
144 foregroundRoleName)
145DEFINE_ROLE_PROPERTIES(checkState, setCheckState, checkStateChanged,
146 checkStateRoleName)
147
148DEFINE_ROLE_PROPERTIES(accessibleText, setAccessibleText, accessibleTextChanged,
149 accessibleTextRoleName)
150DEFINE_ROLE_PROPERTIES(accessibleDescription, setAccessibleDescription, accessibleDescriptionChanged,
151 accessibleDescriptionRoleName)
152
153DEFINE_ROLE_PROPERTIES(sizeHint, setSizeHint, sizeHintChanged,
154 sizeHintRoleName)
155
156QJSValue QQmlTableModelColumn::getterAtRole(const QString &roleName)
157{
158 auto it = mGetters.find(roleName);
159 if (it == mGetters.end())
160 return QJSValue();
161 return *it;
162}
163
164const QHash<QString, QJSValue> QQmlTableModelColumn::getters() const
165{
166 return mGetters;
167}
168
169const QHash<int, QString> QQmlTableModelColumn::supportedRoleNames()
170{
171 static const QHash<int, QString> names {
172 {Qt::DisplayRole, displayRoleName},
173 {Qt::DecorationRole, decorationRoleName},
174 {Qt::EditRole, editRoleName},
175 {Qt::ToolTipRole, toolTipRoleName},
176 {Qt::StatusTipRole, statusTipRoleName},
177 {Qt::WhatsThisRole, whatsThisRoleName},
178 {Qt::FontRole, fontRoleName},
179 {Qt::TextAlignmentRole, textAlignmentRoleName},
180 {Qt::BackgroundRole, backgroundRoleName},
181 {Qt::ForegroundRole, foregroundRoleName},
182 {Qt::CheckStateRole, checkStateRoleName},
183 {Qt::AccessibleTextRole, accessibleTextRoleName},
184 {Qt::AccessibleDescriptionRole, accessibleDescriptionRoleName},
185 {Qt::SizeHintRole, sizeHintRoleName}
186 };
187 return names;
188}
189
190QT_END_NAMESPACE
191
192#include "moc_qqmltablemodelcolumn_p.cpp"
static constexpr QLatin1StringView accessibleDescriptionRoleName("accessibleDescription")
static constexpr QLatin1StringView decorationRoleName("decoration")
static constexpr QLatin1StringView backgroundRoleName("background")
#define DEFINE_ROLE_PROPERTIES(getterGetterName, getterSetterName, getterSignal, roleName)
static constexpr QLatin1StringView whatsThisRoleName("whatsThis")
static constexpr QLatin1StringView editRoleName("edit")
static constexpr QLatin1StringView statusTipRoleName("statusTip")
static QT_BEGIN_NAMESPACE constexpr QLatin1StringView displayRoleName("display")
Represents a column in a model.
static constexpr QLatin1StringView checkStateRoleName("checkState")
static constexpr QLatin1StringView fontRoleName("font")
static constexpr QLatin1StringView foregroundRoleName("foreground")
static constexpr QLatin1StringView sizeHintRoleName("sizeHint")
static constexpr QLatin1StringView accessibleTextRoleName("accessibleText")
static constexpr QLatin1StringView toolTipRoleName("toolTip")
static constexpr QLatin1StringView textAlignmentRoleName("textAlignment")