213void QQmlContext::setContextProperty(
const QString &name,
const QVariant &value)
216 if (d->notifyIndex() == -1)
217 d->setNotifyIndex(QMetaObjectPrivate::absoluteSignalCount(&QQmlContext::staticMetaObject));
219 QQmlRefPointer<QQmlContextData> data = d->m_data;
221 if (data->isInternal()) {
222 qWarning(
"QQmlContext: Cannot set property on internal context.");
226 if (!data->isValid()) {
227 qWarning(
"QQmlContext: Cannot set property on invalid context.");
231 if (
bool isNumber =
false; name.toUInt(&isNumber), isNumber) {
232 qWarning(
"QQmlContext: Using numbers as context properties will be disallowed in a future Qt version.");
236 int idx = data->propertyIndex(name);
238 data->addPropertyNameAndIndex(name, data->numIdValues() + d->numPropertyValues());
239 d->appendPropertyValue(value);
240 data->refreshExpressions();
242 d->setPropertyValue(idx, value);
243 QMetaObject::activate(
this, d->notifyIndex(), idx,
nullptr);
246 if (
auto *obj = qvariant_cast<QObject *>(value)) {
247 connect(obj, &QObject::destroyed,
this, [d, name](QObject *destroyed) {
248 d->dropDestroyedQObject(name, destroyed);
280void QQmlContext::setContextProperties(
const QList<PropertyPair> &properties)
282 Q_D(
const QQmlContext);
284 QQmlRefPointer<QQmlContextData> data = d->m_data;
285 QQmlJavaScriptExpression *expressions = data->takeExpressions();
286 QQmlRefPointer<QQmlContextData> childContexts = data->takeChildContexts();
288 for (
const auto &property : properties)
289 setContextProperty(property.name, property.value);
291 data->setExpressions(expressions);
292 data->setChildContexts(childContexts);
293 data->refreshExpressions();
309 const QQmlRefPointer<QQmlContextData> &data, QObject *object,
const QString &name,
312 QQmlPropertyData local;
313 if (
const QQmlPropertyData *property = QQmlPropertyCache::property(object, name, data, &local)) {
314 *target = object->metaObject()->property(property->coreIndex()).read(object);
331QVariant QQmlContext::contextProperty(
const QString &name)
const
333 Q_D(
const QQmlContext);
335 const QQmlRefPointer<QQmlContextData> data = d->m_data;
337 const int idx = data->propertyIndex(name);
339 if (QObject *obj = data->contextObject()) {
341 if (readObjectProperty(data, obj, name, &value))
346 return parentContext()->contextProperty(name);
348 if (idx >= d->numPropertyValues())
349 return QVariant::fromValue(data->idValue(idx - d->numPropertyValues()));
351 return d->propertyValue(idx);
391QObject *QQmlContext::objectForName(
const QString &name)
const
393 Q_D(
const QQmlContext);
395 QQmlRefPointer<QQmlContextData> data = d->m_data;
396 if (
const int propertyIndex = data->propertyIndex(name); propertyIndex >= 0) {
397 const int numPropertyValues = d->numPropertyValues();
398 if (propertyIndex < numPropertyValues)
399 return qvariant_cast<QObject *>(d->propertyValue(propertyIndex));
400 return data->idValue(propertyIndex - numPropertyValues);
403 if (QObject *obj = data->contextObject()) {
405 if (readObjectProperty(data, obj, name, &result))
406 return qvariant_cast<QObject *>(result);
452QJSValue QQmlContext::importedScript(
const QString &name)
const
454 Q_D(
const QQmlContext);
456 QV4::ExecutionEngine *v4 = engine()->handle();
457 QQmlTypeNameCache::Result r = d->m_data->imports()->query(name, v4->typeLoader());
458 QV4::Scope scope(v4);
459 QV4::ScopedObject scripts(scope, d->m_data->importedScripts());
460 return scripts ? QJSValuePrivate::fromReturnedValue(scripts->get(r.scriptIndex))
461 : QJSValue(QJSValue::UndefinedValue);
466 QQmlContext *context =
static_cast<QQmlContext*>(prop->object);
468 int contextProperty = (
int)(quintptr)prop->data;
470 if (d->propertyValue(contextProperty).userType() != qMetaTypeId<QList<QObject*> >())
473 return ((
const QList<QObject> *)d->propertyValue(contextProperty).constData())->size();
478 QQmlContext *context =
static_cast<QQmlContext*>(prop->object);
480 int contextProperty = (
int)(quintptr)prop->data;
482 if (d->propertyValue(contextProperty).userType() != qMetaTypeId<QList<QObject*> >())
485 return ((
const QList<QObject*> *)d->propertyValue(contextProperty).constData())->at(index);
490 if (!m_data->isValid())
493 const int idx = m_data->propertyIndex(name);
495 if (qvariant_cast<QObject *>(propertyValue(idx)) != destroyed)
498 setPropertyValue(idx, QVariant::fromValue<QObject *>(
nullptr));
499 QMetaObject::activate(q_func(), notifyIndex(), idx,
nullptr);
511 QQmlContext *publicContext,
const QQmlRefPointer<QQmlContextData> &parent,
static bool readObjectProperty(const QQmlRefPointer< QQmlContextData > &data, QObject *object, const QString &name, QVariant *target)