Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qqmldelegatecomponent.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 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
5#include <QtQmlModels/private/qqmladaptormodel_p.h>
6
8
12
28{
29 return m_value;
30}
31
33{
34 if (m_value == value)
35 return;
36 m_value = value;
38 emit changed();
39}
40
61{
62 return m_row;
63}
64
66{
67 if (m_row == r)
68 return;
69 m_row = r;
72 emit changed();
73}
74
80{
81 return m_column;
82}
83
85{
86 if (m_column == c)
87 return;
88 m_column = c;
90 emit changed();
91}
92
94{
95 return m_delegate;
96}
97
116
118{
119 if (!m_value.isValid() && m_row < 0 && m_column < 0)
120 return true;
121
122 bool roleMatched = true;
123 if (m_value.isValid()) {
124 roleMatched = (value == m_value);
125 if (!roleMatched) {
126 bool valueOk = false;
127 bool mValueOk = false;
128 roleMatched = (value.toInt(&valueOk) == m_value.toInt(&mValueOk) && valueOk && mValueOk);
129 }
130 if (!roleMatched)
131 roleMatched = (value.toString() == m_value.toString());
132 }
133 const bool rowMatched = (m_row < 0 ) ? true : m_row == row;
134 const bool columnMatched = (m_column < 0 ) ? true : m_column == column;
135 return roleMatched && rowMatched && columnMatched;
136}
137
141
203{
204 if (m_role == role)
205 return;
206 m_role = role;
208}
209
224QQmlListProperty<QQmlDelegateChoice> QQmlDelegateChooser::choices()
225{
226 return QQmlListProperty<QQmlDelegateChoice>(this, nullptr,
233}
234
235void QQmlDelegateChooser::choices_append(QQmlListProperty<QQmlDelegateChoice> *prop, QQmlDelegateChoice *choice)
236{
237 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
238 q->m_choices.append(choice);
240 emit q->delegateChanged();
241}
242
243qsizetype QQmlDelegateChooser::choices_count(QQmlListProperty<QQmlDelegateChoice> *prop)
244{
245 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser*>(prop->object);
246 return q->m_choices.size();
247}
248
249QQmlDelegateChoice *QQmlDelegateChooser::choices_at(QQmlListProperty<QQmlDelegateChoice> *prop, qsizetype index)
250{
251 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser*>(prop->object);
252 return q->m_choices.at(index);
253}
254
255void QQmlDelegateChooser::choices_clear(QQmlListProperty<QQmlDelegateChoice> *prop)
256{
257 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
258 for (QQmlDelegateChoice *choice : q->m_choices)
260 q->m_choices.clear();
261 emit q->delegateChanged();
262}
263
264void QQmlDelegateChooser::choices_replace(QQmlListProperty<QQmlDelegateChoice> *prop,
266{
267 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
270 q->m_choices[index] = choice;
273 emit q->delegateChanged();
274}
275
276void QQmlDelegateChooser::choices_removeLast(QQmlListProperty<QQmlDelegateChoice> *prop)
277{
278 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
279 disconnect(q->m_choices.takeLast(), &QQmlDelegateChoice::changed,
281 emit q->delegateChanged();
282}
283
285{
286 QVariant v;
287 if (!m_role.isNull())
288 v = value(adaptorModel, row, column, m_role);
289 if (!v.isValid()) { // check if the row only has modelData, for example if the row is a QVariantMap
290 v = value(adaptorModel, row, column, QStringLiteral("modelData"));
291
292 if (v.isValid()) {
293 if (v.canConvert(QMetaType(QMetaType::QVariantMap)))
294 v = v.toMap().value(m_role);
295 else if (v.canConvert(QMetaType(QMetaType::QObjectStar)))
296 v = v.value<QObject*>()->property(m_role.toUtf8());
297 }
298 }
299
300 // loop through choices, finding first one that fits
301 for (int i = 0; i < m_choices.size(); ++i) {
302 const QQmlDelegateChoice *choice = m_choices.at(i);
303 if (choice->match(row, column, v))
304 return choice->delegate();
305 }
306
307 return nullptr;
308}
309
311
312#include "moc_qqmldelegatecomponent_p.cpp"
qsizetype size() const noexcept
Definition qlist.h:397
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
void append(parameter_type t)
Definition qlist.h:458
void clear()
Definition qlist.h:434
\inmodule QtCore
Definition qmetatype.h:341
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
The QQmlComponent class encapsulates a QML component definition.
void setRoleValue(const QVariant &roleValue)
virtual bool match(int row, int column, const QVariant &value) const
void setDelegate(QQmlComponent *delegate)
\qmlproperty Component Qt.labs.qmlmodels::DelegateChoice::delegate This property holds the delegate t...
void setRole(const QString &role)
Allows a view to use different delegates for different types of items in the model.
static void choices_removeLast(QQmlListProperty< QQmlDelegateChoice > *)
static void choices_clear(QQmlListProperty< QQmlDelegateChoice > *)
static void choices_replace(QQmlListProperty< QQmlDelegateChoice > *, qsizetype, QQmlDelegateChoice *)
QQmlComponent * delegate(QQmlAdaptorModel *adaptorModel, int row, int column=-1) const override
static qsizetype choices_count(QQmlListProperty< QQmlDelegateChoice > *)
static QQmlDelegateChoice * choices_at(QQmlListProperty< QQmlDelegateChoice > *, qsizetype)
QQmlListProperty< QQmlDelegateChoice > choices
\qmlproperty list<DelegateChoice> Qt.labs.qmlmodels::DelegateChooser::choices \qmldefault
static void choices_append(QQmlListProperty< QQmlDelegateChoice > *, QQmlDelegateChoice *)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition qstring.h:994
QByteArray toUtf8() const &
Definition qstring.h:634
\inmodule QtCore
Definition qvariant.h:65
bool isValid() const
Returns true if the storage type of this variant is not QMetaType::UnknownType; otherwise returns fal...
Definition qvariant.h:714
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
Combined button and popup list for selecting options.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLsizei const GLfloat * v
[13]
GLuint index
[2]
GLboolean r
[2]
GLenum GLenum GLsizei void GLsizei void * column
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLsizei void * row
#define QStringLiteral(str)
#define emit
ptrdiff_t qsizetype
Definition qtypes.h:165
const char property[13]
Definition qwizard.cpp:101
myObject disconnect()
[26]