Qt 6.x
The Qt SDK
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
103{
104 if (m_delegate == delegate)
105 return;
106 QQmlAbstractDelegateComponent *adc = static_cast<QQmlAbstractDelegateComponent *>(m_delegate);
107 if (adc)
109 m_delegate = delegate;
110 adc = static_cast<QQmlAbstractDelegateComponent *>(delegate);
111 if (adc)
114 emit changed();
115}
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
199{
200 if (m_role == role)
201 return;
202 m_role = role;
204}
205
221{
222 return QQmlListProperty<QQmlDelegateChoice>(this, nullptr,
229}
230
232{
233 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
234 q->m_choices.append(choice);
236 q->delegateChanged();
237}
238
240{
241 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser*>(prop->object);
242 return q->m_choices.size();
243}
244
246{
247 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser*>(prop->object);
248 return q->m_choices.at(index);
249}
250
252{
253 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
254 for (QQmlDelegateChoice *choice : q->m_choices)
256 q->m_choices.clear();
257 q->delegateChanged();
258}
259
262{
263 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
266 q->m_choices[index] = choice;
269 q->delegateChanged();
270}
271
273{
274 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
275 disconnect(q->m_choices.takeLast(), &QQmlDelegateChoice::changed,
277 q->delegateChanged();
278}
279
281{
282 QVariant v;
283 if (!m_role.isNull())
284 v = value(adaptorModel, row, column, m_role);
285 if (!v.isValid()) { // check if the row only has modelData, for example if the row is a QVariantMap
286 v = value(adaptorModel, row, column, QStringLiteral("modelData"));
287
288 if (v.isValid()) {
289 if (v.canConvert(QMetaType(QMetaType::QVariantMap)))
290 v = v.toMap().value(m_role);
291 else if (v.canConvert(QMetaType(QMetaType::QObjectStar)))
292 v = v.value<QObject*>()->property(m_role.toUtf8());
293 }
294 }
295
296 // loop through choices, finding first one that fits
297 for (int i = 0; i < m_choices.size(); ++i) {
298 const QQmlDelegateChoice *choice = m_choices.at(i);
299 if (choice->match(row, column, v))
300 return choice->delegate();
301 }
302
303 return nullptr;
304}
305
307
308#include "moc_qqmldelegatecomponent_p.cpp"
qsizetype size() const noexcept
Definition qlist.h:386
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
void append(parameter_type t)
Definition qlist.h:441
\inmodule QtCore
Definition qmetatype.h:320
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
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 *)
The QQmlListProperty class allows applications to expose list-like properties of QObject-derived clas...
Definition qqmllist.h:24
QObject * object
Definition qqmllist.h:81
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition qstring.h:898
QByteArray toUtf8() const &
Definition qstring.h:563
\inmodule QtCore
Definition qvariant.h:64
bool isValid() const
Returns true if the storage type of this variant is not QMetaType::UnknownType; otherwise returns fal...
Definition qvariant.h:707
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:70
const char property[13]
Definition qwizard.cpp:101
myObject disconnect()
[26]