Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmlcontext.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
4#include "qqmlcontext_p.h"
6
7#include "qqmlcomponent_p.h"
8#include "qqmlexpression_p.h"
9#include "qqmlengine_p.h"
10#include "qqmlengine.h"
11#include "qqmlinfo.h"
13
14#include <qjsengine.h>
15#include <QtCore/qvarlengtharray.h>
16#include <private/qmetaobject_p.h>
17#include <QtCore/qdebug.h>
18
20
91{
92}
93
100 ? QQmlContextData::get(engine->rootContext())
102{
103}
104
110 : QObject(*(new QQmlContextPrivate(this, parentContext
111 ? QQmlContextData::get(parentContext)
113{
114}
115
120 : QObject(dd, parent)
121{
122}
123
132{
133 Q_D(QQmlContext);
134 d->m_data->clearPublicContext();
135}
136
144{
145 Q_D(const QQmlContext);
146 return d->m_data->isValid();
147}
148
154{
155 Q_D(const QQmlContext);
156 return d->m_data->engine();
157}
158
164{
165 Q_D(const QQmlContext);
166
167 if (QQmlRefPointer<QQmlContextData> parent = d->m_data->parent())
168 return parent->asQQmlContext();
169 return nullptr;
170}
171
176{
177 Q_D(const QQmlContext);
178 return d->m_data->contextObject();
179}
180
188{
189 Q_D(QQmlContext);
190
192
193 if (data->isInternal()) {
194 qWarning("QQmlContext: Cannot set context object for internal context.");
195 return;
196 }
197
198 if (!data->isValid()) {
199 qWarning("QQmlContext: Cannot set context object on invalid context.");
200 return;
201 }
202
203 data->setContextObject(object);
204 data->refreshExpressions();
205}
206
214{
215 Q_D(QQmlContext);
216 if (d->notifyIndex() == -1)
217 d->setNotifyIndex(QMetaObjectPrivate::absoluteSignalCount(&QQmlContext::staticMetaObject));
218
220
221 if (data->isInternal()) {
222 qWarning("QQmlContext: Cannot set property on internal context.");
223 return;
224 }
225
226 if (!data->isValid()) {
227 qWarning("QQmlContext: Cannot set property on invalid context.");
228 return;
229 }
230
231 int idx = data->propertyIndex(name);
232 if (idx == -1) {
233 data->addPropertyNameAndIndex(name, data->numIdValues() + d->numPropertyValues());
234 d->appendPropertyValue(value);
235 data->refreshExpressions();
236 } else {
237 d->setPropertyValue(idx, value);
238 QMetaObject::activate(this, d->notifyIndex(), idx, nullptr);
239 }
240
241 if (auto *obj = qvariant_cast<QObject *>(value)) {
243 d->dropDestroyedQObject(name, destroyed);
244 });
245 }
246}
247
257{
259}
260
276{
277 Q_D(const QQmlContext);
278
280 QQmlJavaScriptExpression *expressions = data->takeExpressions();
281 QQmlRefPointer<QQmlContextData> childContexts = data->takeChildContexts();
282
283 for (const auto &property : properties)
285
286 data->setExpressions(expressions);
287 data->setChildContexts(childContexts);
288 data->refreshExpressions();
289}
290
306{
307 QQmlPropertyData local;
308 if (const QQmlPropertyData *property = QQmlPropertyCache::property(object, name, data, &local)) {
309 *target = object->metaObject()->property(property->coreIndex()).read(object);
310 return true;
311 }
312 return false;
313}
314
327{
328 Q_D(const QQmlContext);
329
330 const QQmlRefPointer<QQmlContextData> data = d->m_data;
331
332 const int idx = data->propertyIndex(name);
333 if (idx == -1) {
334 if (QObject *obj = data->contextObject()) {
337 return value;
338 }
339
340 if (parentContext())
342 } else {
343 if (idx >= d->numPropertyValues())
344 return QVariant::fromValue(data->idValue(idx - d->numPropertyValues()));
345 else
346 return d->propertyValue(idx);
347 }
348
349 return QVariant();
350}
351
367{
368 Q_D(const QQmlContext);
369
370 return d->m_data->findObjectId(object);
371}
372
387{
388 Q_D(const QQmlContext);
389
391 if (const int propertyIndex = data->propertyIndex(name); propertyIndex >= 0) {
392 const int numPropertyValues = d->numPropertyValues();
393 if (propertyIndex < numPropertyValues)
394 return qvariant_cast<QObject *>(d->propertyValue(propertyIndex));
395 return data->idValue(propertyIndex - numPropertyValues);
396 }
397
398 if (QObject *obj = data->contextObject()) {
401 return qvariant_cast<QObject *>(result);
402 }
403
404 return nullptr;
405}
406
414{
415 Q_D(const QQmlContext);
416 return d->m_data->resolvedUrl(src);
417}
418
428{
429 Q_D(QQmlContext);
430 d->m_data->setBaseUrl(baseUrl);
431 d->m_data->setBaseUrlString(baseUrl.toString());
432}
433
439{
440 Q_D(const QQmlContext);
441 return d->m_data->baseUrl();
442}
443
448{
449 Q_D(const QQmlContext);
450
451 QQmlTypeNameCache::Result r = d->m_data->imports()->query(name);
452 QV4::Scope scope(engine()->handle());
453 QV4::ScopedObject scripts(scope, d->m_data->importedScripts().valueRef());
454 return scripts ? QJSValuePrivate::fromReturnedValue(scripts->get(r.scriptIndex))
456}
457
459{
460 QQmlContext *context = static_cast<QQmlContext*>(prop->object);
462 int contextProperty = (int)(quintptr)prop->data;
463
464 if (d->propertyValue(contextProperty).userType() != qMetaTypeId<QList<QObject*> >())
465 return 0;
466 else
467 return ((const QList<QObject> *)d->propertyValue(contextProperty).constData())->size();
468}
469
471{
472 QQmlContext *context = static_cast<QQmlContext*>(prop->object);
474 int contextProperty = (int)(quintptr)prop->data;
475
476 if (d->propertyValue(contextProperty).userType() != qMetaTypeId<QList<QObject*> >())
477 return nullptr;
478 else
479 return ((const QList<QObject*> *)d->propertyValue(contextProperty).constData())->at(index);
480}
481
483{
484 if (!m_data->isValid())
485 return;
486
487 const int idx = m_data->propertyIndex(name);
488 Q_ASSERT(idx >= 0);
489 if (qvariant_cast<QObject *>(propertyValue(idx)) != destroyed)
490 return;
491
492 setPropertyValue(idx, QVariant::fromValue<QObject *>(nullptr));
493 QMetaObject::activate(q_func(), notifyIndex(), idx, nullptr);
494}
495
497{
498 m_data->emitDestruction();
499}
500
501// m_data is owned by the public context. When the public context is reset to nullptr, it will be
502// deref'd. It's OK to pass a half-created publicContext here. We will not dereference it during
503// construction.
504QQmlContextPrivate::QQmlContextPrivate(
507 m_data(new QQmlContextData(QQmlContextData::OwnedByPublicContext, publicContext,
508 parent, engine))
509{
510 Q_ASSERT(publicContext != nullptr);
511}
512
514
515#include "moc_qqmlcontext.cpp"
NSData * m_data
static QJSValue fromReturnedValue(QV4::ReturnedValue d)
Definition qjsvalue_p.h:189
The QJSValue class acts as a container for Qt/JavaScript data types.
Definition qjsvalue.h:31
@ UndefinedValue
Definition qjsvalue.h:35
Definition qlist.h:74
\inmodule QtCore
Definition qobject.h:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
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
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
int propertyIndex(const QString &name) const
bool isValid() const
void dropDestroyedQObject(const QString &name, QObject *destroyed)
static qsizetype context_count(QQmlListProperty< QObject > *)
void setPropertyValue(int index, const QVariant &value)
int notifyIndex() const
static QQmlContextPrivate * get(QQmlContext *context)
QVariant propertyValue(int index) const
static QObject * context_at(QQmlListProperty< QObject > *, qsizetype)
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
QString nameForObject(const QObject *) const
Returns the name of object in this context, or an empty string if object is not named in the context.
bool isValid() const
Returns whether the context is valid.
void setBaseUrl(const QUrl &)
Explicitly sets the url resolvedUrl() will use for relative references to baseUrl.
QUrl baseUrl() const
Returns the base url of the component, or the containing component if none is set.
QQmlContext(QQmlEngine *parent, QObject *objParent=nullptr)
Create a new QQmlContext as a child of engine's root context, and the QObject parent.
QUrl resolvedUrl(const QUrl &) const
Resolves the URL src relative to the URL of the containing component.
QVariant contextProperty(const QString &) const
Returns the value of the name property for this context as a QVariant.
QJSValue importedScript(const QString &name) const
QQmlContext * parentContext() const
Return the context's parent QQmlContext, or \nullptr if this context has no parent or if the parent h...
QObject * objectForName(const QString &) const
QObject * contextObject() const
Return the context object, or \nullptr if there is no context object.
void setContextProperty(const QString &, QObject *)
Set the value of the name property on this context.
void setContextObject(QObject *)
Set the context object.
QQmlEngine * engine() const
Return the context's QQmlEngine, or \nullptr if the context has no QQmlEngine or the QQmlEngine was d...
~QQmlContext() override
Destroys the QQmlContext.
void setContextProperties(const QList< PropertyPair > &properties)
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
The QQmlListProperty class allows applications to expose list-like properties of QObject-derived clas...
Definition qqmllist.h:24
QObject * object
Definition qqmllist.h:81
const QQmlPropertyData * property(const K &key, QObject *object, const QQmlRefPointer< QQmlContextData > &context) const
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qurl.h:94
QString toString(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
Definition qurl.cpp:2828
\inmodule QtCore
Definition qvariant.h:64
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:531
#define this
Definition dialogs.cpp:9
double e
Combined button and popup list for selecting options.
static void * context
static const QCssKnownValue properties[NumProperties - 1]
static QDBusError::ErrorType get(const char *name)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qWarning
Definition qlogging.h:162
GLuint64 GLenum void * handle
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLboolean r
[2]
GLenum src
GLenum target
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
GLhandleARB obj
[2]
GLuint64EXT * result
[6]
static bool readObjectProperty(const QQmlRefPointer< QQmlContextData > &data, QObject *object, const QString &name, QVariant *target)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
size_t quintptr
Definition qtypes.h:72
ptrdiff_t qsizetype
Definition qtypes.h:70
const char property[13]
Definition qwizard.cpp:101
QUrl baseUrl
QAction * at
QJSEngine engine
[0]
static Q_CORE_EXPORT int absoluteSignalCount(const QMetaObject *m)
static void activate(QObject *sender, int signal_index, void **argv)
Definition qobject.cpp:4057
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent