Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
specialize.cpp
Go to the documentation of this file.
1
// Copyright (C) 2025 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4
#
include
<
QtCore
/
qrangemodel
.
h
>
5
6
#
ifndef
QT_NO_WIDGETS
7
8
#
include
<
QtCore
/
qiodevice
.
h
>
9
#
include
<
QtCore
/
qmimedata
.
h
>
10
#
include
<
QtCore
/
qstring
.
h
>
11
#
include
<
QtCore
/
qtextstream
.
h
>
12
#
include
<
QtCore
/
qxmlstream
.
h
>
13
#
include
<
QtGui
/
qcolor
.
h
>
14
15
#
include
<
QtWidgets
/
qlistview
.
h
>
16
#
include
<
QtWidgets
/
qtableview
.
h
>
17
18
using
namespace
Qt::StringLiterals;
19
20
namespace
multirole_gadget
{
21
//! [color_gadget_decl]
22
class
ColorEntry
23
{
24
Q_GADGET
25
Q_PROPERTY(QString display READ colorName)
26
Q_PROPERTY
(
QColor
decoration
READ
decoration
)
27
Q_PROPERTY
(
QString
toolTip
READ
toolTip
)
28
public
:
29
//! [color_gadget_decl]
30
Qt
::
ItemFlags
flags
()
const
{
return
{}; }
31
//! [color_gadget_impl]
32
ColorEntry
(
const
QString
&
color
= {})
33
:
m_colorName
(
color
)
34
{}
35
36
QString
colorName
()
const
37
{
38
return
m_colorName
;
39
}
40
QColor
decoration
()
const
41
{
42
return
QColor
::
fromString
(
m_colorName
);
43
}
44
QString
toolTip
()
const
45
{
46
return
QColor
::
fromString
(
m_colorName
).
name
();
47
}
48
49
private
:
50
QString
m_colorName
;
51
//! [color_gadget_impl]
52
//! [color_gadget_end]
53
};
54
//! [color_gadget_end]
55
}
56
57
using
ColorEntry
=
multirole_gadget
::
ColorEntry
;
58
//! [color_gadget_row_options_decl]
59
60
template
<>
61
struct
QRangeModel::RowOptions<ColorEntry>
62
{
63
//! [color_gadget_row_options_decl]
64
//! [color_gadget_row_options_rowCategory]
65
static
constexpr
auto
rowCategory = QRangeModel::RowCategory::MultiRoleItem;
66
//! [color_gadget_row_options_rowCategory]
67
//! [color_gadget_row_options_headerData]
68
static
QVariant headerData(
int
section,
int
role)
69
{
70
switch
(section) {
71
// ...
72
}
73
return
{};
74
}
75
//! [color_gadget_row_options_headerData]
76
//! [color_gadget_row_options_flags]
77
static
Qt::ItemFlags flags(
const
ColorEntry &entry)
78
{
79
return
entry.flags();
80
}
81
//! [color_gadget_row_options_flags]
82
//! [color_gadget_row_options_mimeTypes]
83
static
QStringList mimeTypes()
84
{
85
return
{
86
u"text/html"_s
87
};
88
}
89
//! [color_gadget_row_options_mimeTypes]
90
//! [color_gadget_row_options_mimeData]
91
template
<
typename
Items>
92
static
QMimeData *mimeData(
const
Items &items)
93
{
94
if
(items.isEmpty())
95
return
nullptr
;
96
QByteArray data;
97
QXmlStreamWriter stream(&data);
98
stream.writeStartElement(
"ul"
);
99
for
(
const
auto
&[item, index] : items)
100
stream.writeTextElement(
"li"
, item.colorName());
101
stream.writeEndElement();
102
103
QMimeData *mimeData =
new
QMimeData;
104
mimeData->setData(mimeTypes().first(), data);
105
return
mimeData;
106
}
107
//! [color_gadget_row_options_mimeData]
108
//! [color_gadget_row_options_canDropMimeData]
109
static
bool
canDropMimeData(
const
QMimeData *mimeData)
110
{
111
// ...
112
return
true
;
113
}
114
//! [color_gadget_row_options_canDropMimeData]
115
//! [color_gadget_row_options_dropMimeData]
116
static
bool
dropMimeData(
const
QMimeData *mimeData,
auto
inserter)
117
{
118
const
QByteArray data = mimeData->data(mimeTypes().first());
119
if
(data.isEmpty())
120
return
false
;
121
QXmlStreamReader stream(data);
122
while
(!stream.atEnd()) {
123
stream.readNext();
124
if
(stream.isStartElement() && stream.name() == u"li"_s)
125
inserter = ColorEntry(stream.readElementText());
126
}
127
128
return
true
;
129
}
130
//! [color_gadget_row_options_dropMimeData]
131
//! [color_gadget_row_options_end_decl]
132
};
133
//! [color_gadget_row_options_end_decl]
134
//! [color_gadget_row_options]
135
136
//! [color_gadget_item_access_decl]
137
template
<>
138
struct
QRangeModel::ItemAccess<ColorEntry>
139
{
140
//! [color_gadget_item_access_decl]
141
//! [color_gadget_item_access_flags]
142
static
Qt::ItemFlags flags(
const
ColorEntry &entry)
143
{
144
return
entry.flags();
145
}
146
//! [color_gadget_item_access_flags]
147
//! [color_gadget_item_access_readRole]
148
static
QVariant readRole(
const
ColorEntry &entry,
int
role)
149
{
150
switch
(role) {
151
// ...
152
}
153
return
{};
154
}
155
//! [color_gadget_item_access_readRole]
156
//! [color_gadget_item_access_writeRole]
157
static
bool
writeRole(ColorEntry &entry,
const
QVariant &value,
int
role)
158
{
159
bool
ok =
false
;
160
switch
(role) {
161
// ...
162
}
163
return
ok;
164
}
165
//! [color_gadget_item_access_writeRole]
166
//! [color_gadget_item_access_mimeTypes]
167
static
QStringList mimeTypes()
168
{
169
return
{
170
u"text/plain"_s,
171
u"application/x-qabstractitemmodeldatalist"_s
172
};
173
}
174
//! [color_gadget_item_access_mimeTypes]
175
//! [color_gadget_item_access_mimeData]
176
static
QMimeData *mimeData(
const
auto
&range)
177
{
178
QByteArray data;
179
QTextStream stream(&data, QIODevice::WriteOnly);
180
for
(
const
auto
&[item, index] : range)
181
stream << item.colorName() << Qt::endl;
182
183
QMimeData *result =
new
QMimeData;
184
result->setData(mimeTypes().first(), data);
185
// Qt handles encoding into the default mime type
186
return
result;
187
}
188
//! [color_gadget_item_access_mimeData]
189
//! [color_gadget_item_access_canDropMimeData]
190
static
bool
canDropMimeData(
const
QMimeData *mimeData)
191
{
192
// ...
193
return
true
;
194
}
195
//! [color_gadget_item_access_canDropMimeData]
196
//! [color_gadget_item_access_dropMimeData]
197
static
bool
dropMimeData(
const
QMimeData *mimeData,
auto
inserter)
198
{
199
QByteArray data = mimeData->data(mimeTypes().first());
200
if
(data.isEmpty())
201
return
false
;
202
QTextStream stream(&data, QIODevice::ReadOnly);
203
while
(!stream.atEnd()) {
204
QString colorName;
205
stream >> colorName;
206
inserter = ColorEntry(colorName);
207
}
208
return
true
;
209
}
210
//! [color_gadget_item_access_dropMimeData]
211
//! [color_gadget_item_access_end_decl]
212
};
213
//! [color_gadget_item_access_end_decl]
214
215
namespace
multirole_gadget
{
216
void
color_table
() {
217
//! [color_gadget_table]
218
QList<QList<ColorEntry>> colorTable;
219
220
// ...
221
222
QRangeModel colorModel(colorTable);
223
QTableView table;
224
table.setModel(&colorModel);
225
//! [color_gadget_table]
226
}
227
228
void
color_list_multi_role
() {
229
//! [color_gadget_multi_role]
230
const
QStringList colorNames = QColor::colorNames();
231
QList<ColorEntry> colors;
232
colors.reserve(colorNames.size());
233
for
(
const
QString &name : colorNames)
234
colors << name;
235
236
QRangeModel colorModel(colors);
237
QListView list;
238
list.setModel(&colorModel);
239
//! [color_gadget_multi_role]
240
}
241
242
void
color_list_pointers
()
243
{
244
//! [color_gadget_item_access_use]
245
246
std::vector<std::shared_ptr<ColorEntry>> colors;
247
// ...
248
QRangeModel model(colors);
249
//! [color_gadget_item_access_use]
250
};
251
252
void
color_list_single_column
() {
253
//! [color_gadget_single_column]
254
const
QStringList colorNames = QColor::colorNames();
255
QList<std::tuple<ColorEntry>> colors;
256
257
// ...
258
259
QRangeModel colorModel(colors);
260
QListView list;
261
list.setModel(&colorModel);
262
//! [color_gadget_single_column]
263
264
{
265
//! [color_gadget_single_column_access_get]
266
ColorEntry
firstEntry = std::get<0>(colors.at(0));
267
//! [color_gadget_single_column_access_get]
268
}
269
{
270
//! [color_gadget_single_column_access_sb]
271
auto
[firstEntry] = colors.at(0);
272
//! [color_gadget_single_column_access_sb]
273
}
274
}
275
}
// namespace multirole_gadget
276
277
#
endif
//QT_NO_WIDGETS
multirole_gadget::ColorEntry
[color_gadget_decl]
Definition
specialize.cpp:23
multirole_gadget
[color_gadget_item_access_end_decl]
Definition
specialize.cpp:20
multirole_gadget::color_list_single_column
void color_list_single_column()
Definition
specialize.cpp:252
multirole_gadget::color_list_pointers
void color_list_pointers()
Definition
specialize.cpp:242
multirole_gadget::color_list_multi_role
void color_list_multi_role()
Definition
specialize.cpp:228
multirole_gadget::color_table
void color_table()
Definition
specialize.cpp:216
ColorEntry
multirole_gadget::ColorEntry ColorEntry
Definition
specialize.cpp:57
qtbase
src
corelib
doc
snippets
qrangemodel
specialize.cpp
Generated on
for Qt by
1.18.0