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
qqmlglobal.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 <QtQml/private/qjsvalue_p.h>
5#include <QtQml/private/qqmlglobal_p.h>
6#include <QtQml/private/qqmlmetatype_p.h>
7#include <QtQml/qqmlengine.h>
8
9#include <QtCore/private/qvariant_p.h>
10#include <QtCore/qcoreapplication.h>
11#include <QtCore/qdebug.h>
12#include <QtCore/qstringlist.h>
13
15
16// Pre-filter the metatype before poking QQmlMetaType::qmlType() and locking its mutex.
17static bool isConstructibleMetaType(const QMetaType metaType)
18{
19 switch (metaType.id()) {
20 // The builtins are not constructible this way.
21 case QMetaType::Void:
22 case QMetaType::Nullptr:
24 case QMetaType::Int:
25 case QMetaType::UInt:
26 case QMetaType::LongLong:
27 case QMetaType::ULongLong:
28 case QMetaType::Float:
29 case QMetaType::Double:
30 case QMetaType::Long:
31 case QMetaType::ULong:
32 case QMetaType::Short:
33 case QMetaType::UShort:
34 case QMetaType::Char:
35 case QMetaType::SChar:
36 case QMetaType::UChar:
37 case QMetaType::QChar:
38 case QMetaType::QString:
39 case QMetaType::Bool:
40 case QMetaType::QDateTime:
41 case QMetaType::QDate:
42 case QMetaType::QTime:
43 case QMetaType::QUrl:
44 case QMetaType::QRegularExpression:
45 case QMetaType::QByteArray:
46 case QMetaType::QLocale:
47 return false;
48 default:
49 break;
50 }
51
52 // QJSValue is also builtin
53 if (metaType == QMetaType::fromType<QJSValue>())
54 return false;
55
56 // We also don't want to construct pointers of any kind, or lists, or enums.
57 if (metaType.flags() &
67 return false;
68 }
69
70 return true;
71}
72
74{
75 const QtPrivate::QMetaTypeInterface *iface = type.iface();
77 Q_ASSERT(d->is_null && !d->is_shared);
78 *d = QVariant::Private(iface);
80 return d->data.data;
81
82 // This is not exception safe.
83 // If your value type throws an exception from its ctor bad things will happen anyway.
84 d->data.shared = QVariant::PrivateShared::create(iface->size, iface->alignment);
85 d->is_shared = true;
86 return d->data.shared->data();
87}
88
89static void callConstructor(
90 const QMetaObject *targetMetaObject, int i, void *source, void *target)
91{
92 void *p[] = { target, source };
94}
95
96template<typename Allocate>
97static void fromVerifiedType(
98 const QMetaObject *targetMetaObject, int ctorIndex, void *source, Allocate &&allocate)
99{
100 const QMetaMethod ctor = targetMetaObject->constructor(ctorIndex);
101 Q_ASSERT_X(ctor.parameterCount() == 1, "fromVerifiedType",
102 "Value type constructor must take exactly one argument");
103 callConstructor(targetMetaObject, ctorIndex, source, allocate());
104}
105
106
107template<typename Allocate, typename Retrieve>
109 const QMetaObject *targetMetaObject, Allocate &&allocate, Retrieve &&retrieve)
110{
111 for (int i = 0, end = targetMetaObject->constructorCount(); i < end; ++i) {
112 const QMetaMethod ctor = targetMetaObject->constructor(i);
113 if (ctor.parameterCount() != 1)
114 continue;
115
116 const QMetaType parameterType = ctor.parameterMetaType(0);
117
118 if (retrieve(parameterType, [&](QMetaType sourceMetaType, void *sourceData) {
119 if (sourceMetaType == parameterType) {
120 callConstructor(targetMetaObject, i, sourceData, allocate());
121 return true;
122 }
123
124 if (const QMetaObject *parameterMetaObject = parameterType.metaObject()) {
125 if (const QMetaObject *sourceMetaObject = sourceMetaType.metaObject();
126 sourceMetaObject && sourceMetaObject->inherits(parameterMetaObject)) {
127 // Allow construction from derived types.
128 callConstructor(targetMetaObject, i, sourceData, allocate());
129 return true;
130 }
131 }
132
133 // Do not recursively try to create parameters here. This may end up in infinite recursion.
134
135 // At this point, s should be a builtin type. For builtin types
136 // the QMetaType converters are good enough.
137 QVariant converted(parameterType);
138 if (QMetaType::convert(sourceMetaType, sourceData, parameterType, converted.data())) {
139 callConstructor(targetMetaObject, i, converted.data(), allocate());
140 return true;
141 }
142
143 return false;
144 })) {
145 return true;
146 }
147 }
148
149 return false;
150}
151
152template<typename Allocate>
154 const QMetaObject *targetMetaObject, const QV4::Value &source, Allocate &&allocate)
155{
156 return fromMatchingType(
157 targetMetaObject, std::forward<Allocate>(allocate),
158 [&](QMetaType parameterType, auto callback) {
160 return callback(variant.metaType(), variant.data());
161 });
162}
163
164template<typename Allocate>
166 const QMetaObject *targetMetaObject, QVariant source, Allocate &&allocate)
167{
168 return fromMatchingType(targetMetaObject, std::forward<Allocate>(allocate),
169 [&](QMetaType, auto callback) {
170 return callback(source.metaType(), source.data());
171 });
172}
173
174template<typename Allocate>
175static bool fromString(const QMetaObject *mo, QString s, Allocate &&allocate)
176{
177 for (int i = 0, end = mo->constructorCount(); i < end; ++i) {
178 const QMetaMethod ctor = mo->constructor(i);
179 if (ctor.parameterCount() != 1)
180 continue;
181
182 if (ctor.parameterMetaType(0) == QMetaType::fromType<QString>()) {
183 callConstructor(mo, i, &s, allocate());
184 return true;
185 }
186 }
187
188 return false;
189}
190
191template<typename Get, typename Convert>
192static bool doWriteProperty(const QMetaProperty &metaProperty, void *target,
193 Get &&get, Convert &&convert)
194{
195 const QMetaType propertyType = metaProperty.metaType();
196 QVariant property = get(propertyType);
197 if (property.metaType() == propertyType) {
198 metaProperty.writeOnGadget(target, std::move(property));
199 return true;
200 }
201
202 QVariant converted = convert(propertyType);
203 if (converted.isValid()) {
204 metaProperty.writeOnGadget(target, std::move(converted));
205 return true;
206 }
207
208 converted = QVariant(propertyType);
209 if (QMetaType::convert(property.metaType(), property.constData(),
210 propertyType, converted.data())) {
211 metaProperty.writeOnGadget(target, std::move(converted));
212 return true;
213 }
214
215 return false;
216}
217
219 const QMetaObject *targetMetaObject, void *target, const QV4::Value &source)
220{
221 const QV4::Object *o = static_cast<const QV4::Object *>(&source);
222 QV4::Scope scope(o->engine());
223 QV4::ScopedObject object(scope, o);
224
225 for (int i = 0; i < targetMetaObject->propertyCount(); ++i) {
226 const QMetaProperty metaProperty = targetMetaObject->property(i);
227 const QString propertyName = QString::fromUtf8(metaProperty.name());
228
229 QV4::ScopedString v4PropName(scope, scope.engine->newString(propertyName));
230 QV4::ScopedValue v4PropValue(scope, object->get(v4PropName));
231
232 // We assume that data is freshly constructed.
233 // There is no point in reset()'ing properties of a freshly created object.
234 if (v4PropValue->isUndefined())
235 continue;
236
237 if (doWriteProperty(metaProperty, target, [&](const QMetaType &propertyType) {
238 return QV4::ExecutionEngine::toVariant(v4PropValue, propertyType);
239 }, [&](const QMetaType &propertyType) {
240 return QQmlValueTypeProvider::createValueType(v4PropValue, propertyType);
241 })) {
242 continue;
243 }
244
245 const QMetaType propertyType = metaProperty.metaType();
246 QVariant property = QV4::ExecutionEngine::toVariant(v4PropValue, propertyType);
247 if (property.metaType() == propertyType) {
248 metaProperty.writeOnGadget(target, std::move(property));
249 continue;
250 }
251
252 QVariant converted = QQmlValueTypeProvider::createValueType(v4PropValue, propertyType);
253 if (converted.isValid()) {
254 metaProperty.writeOnGadget(target, std::move(converted));
255 continue;
256 }
257
258 converted = QVariant(propertyType);
259 if (QMetaType::convert(property.metaType(), property.constData(),
260 propertyType, converted.data())) {
261 metaProperty.writeOnGadget(target, std::move(converted));
262 continue;
263 }
264
265 qWarning().noquote()
266 << QLatin1String("Could not convert %1 to %2 for property %3")
267 .arg(v4PropValue->toQStringNoThrow(), QString::fromUtf8(propertyType.name()),
268 propertyName);
269 }
270}
271
273 const QMetaObject *targetMetaObject, QMetaType metaType, const QV4::Value &source)
274{
275 if (!source.isObject() || !targetMetaObject)
276 return QVariant();
277
278 QVariant result(metaType);
279 doWriteProperties(targetMetaObject, result.data(), source);
280 return result;
281}
282
283template<typename Read>
285 const QMetaObject *targetMetaObject, void *target,
286 const QMetaObject *sourceMetaObject, Read &&read)
287{
288 for (int i = 0; i < targetMetaObject->propertyCount(); ++i) {
289 const QMetaProperty metaProperty = targetMetaObject->property(i);
290
291 const int sourceProperty = sourceMetaObject->indexOfProperty(metaProperty.name());
292
293 // We assume that data is freshly constructed.
294 // There is no point in reset()'ing properties of a freshly created object.
295 if (sourceProperty == -1)
296 continue;
297
298 const QMetaType propertyType = metaProperty.metaType();
299 QVariant property = read(sourceMetaObject, sourceProperty);
300 if (property.metaType() == propertyType) {
301 metaProperty.writeOnGadget(target, std::move(property));
302 continue;
303 }
304
306 if (converted.isValid()) {
307 metaProperty.writeOnGadget(target, std::move(converted));
308 continue;
309 }
310
311 converted = QVariant(propertyType);
312 if (QMetaType::convert(property.metaType(), property.constData(),
313 propertyType, converted.data())) {
314 metaProperty.writeOnGadget(target, std::move(converted));
315 continue;
316 }
317
318 qWarning().noquote()
319 << QLatin1String("Could not convert %1 to %2 for property %3")
320 .arg(property.toString(), QString::fromUtf8(propertyType.name()),
321 QString::fromUtf8(metaProperty.name()));
322 }
323}
324
325
326static void doWriteProperties(const QMetaObject *targetMeta, void *target, QObject *source)
327{
329 targetMeta, target, source->metaObject(),
330 [source](const QMetaObject *sourceMetaObject, int sourceProperty) {
331 return sourceMetaObject->property(sourceProperty).read(source);
332 });
333}
334
336 const QMetaObject *targetMetaObject, QMetaType targetMetaType, QObject *source)
337{
338 if (!source || !targetMetaObject)
339 return QVariant();
340
341 QVariant result(targetMetaType);
342 doWriteProperties(targetMetaObject, result.data(), source);
343 return result;
344}
345
347 const QMetaObject *targetMetaObject, QMetaType targetMetaType,
348 const QMetaObject *sourceMetaObject, const void *source)
349{
350 if (!source || !sourceMetaObject || !targetMetaObject)
351 return QVariant();
352
353 QVariant result(targetMetaType);
355 targetMetaObject, result.data(), sourceMetaObject,
356 [source](const QMetaObject *sourceMetaObject, int sourceProperty) {
357 return sourceMetaObject->property(sourceProperty).readOnGadget(source);
358 });
359 return result;
360}
361
362template<typename Map>
363void doWriteProperties(const QMetaObject *targetMetaObject, void *target, const Map &source)
364{
365 for (int i = 0; i < targetMetaObject->propertyCount(); ++i) {
366 const QMetaProperty metaProperty = targetMetaObject->property(i);
367
368 // We assume that data is freshly constructed.
369 // There is no point in reset()'ing properties of a freshly created object.
370 const auto it = source.constFind(QString::fromUtf8(metaProperty.name()));
371 if (it == source.constEnd())
372 continue;
373
374 const QMetaType propertyType = metaProperty.metaType();
375 QVariant property = *it;
376 if (property.metaType() == propertyType) {
377 metaProperty.writeOnGadget(target, std::move(property));
378 continue;
379 }
380
382 if (converted.isValid()) {
383 metaProperty.writeOnGadget(target, std::move(converted));
384 continue;
385 }
386
387 converted = QVariant(propertyType);
388 if (QMetaType::convert(property.metaType(), property.constData(),
389 propertyType, converted.data())) {
390 metaProperty.writeOnGadget(target, std::move(converted));
391 continue;
392 }
393
394 qWarning().noquote()
395 << QLatin1String("Could not convert %1 to %2 for property %3")
396 .arg(property.toString(), QString::fromUtf8(propertyType.name()),
397 QString::fromUtf8(metaProperty.name()));
398 }
399}
400
401template<typename Map>
403 const QMetaObject *targetMetaObject, QMetaType targetMetaType, const Map &source)
404{
405 QVariant result(targetMetaType);
406 doWriteProperties(targetMetaObject, result.data(), source);
407 return result;
408}
409
411 const QMetaObject *targetMetaObject, QMetaType targetMetaType, const QVariant &source)
412{
413 if (!targetMetaObject)
414 return QVariant();
415
416 if (source.metaType() == QMetaType::fromType<QJSValue>()) {
417 QJSValue val = source.value<QJSValue>();
418 return byProperties(
419 targetMetaObject, targetMetaType, QV4::Value(QJSValuePrivate::asReturnedValue(&val)));
420 }
421
422 if (source.metaType() == QMetaType::fromType<QVariantMap>()) {
423 return byProperties(
424 targetMetaObject, targetMetaType,
425 *static_cast<const QVariantMap *>(source.constData()));
426 }
427
428 if (source.metaType() == QMetaType::fromType<QVariantHash>()) {
429 return byProperties(
430 targetMetaObject, targetMetaType,
431 *static_cast<const QVariantHash *>(source.constData()));
432 }
433
434 if (source.metaType().flags() & QMetaType::PointerToQObject)
435 return byProperties(targetMetaObject, targetMetaType, source.value<QObject *>());
436
437 if (const QMetaObject *sourceMeta = QQmlMetaType::metaObjectForValueType(source.metaType()))
438 return byProperties(targetMetaObject, targetMetaType, sourceMeta, source.constData());
439
440 return QVariant();
441}
442
443template<typename Allocate, typename DefaultConstruct>
445 const QQmlType &targetType, const QV4::Value &source,
446 Allocate &&allocate, DefaultConstruct &&defaultConstruct)
447{
448 const auto warn = [&](const QMetaObject *targetMetaObject) {
449 qWarning().noquote()
450 << "Could not find any constructor for value type"
451 << targetMetaObject->className() << "to call with value"
452 << source.toQStringNoThrow();
453 };
454
455 if (targetType.canPopulateValueType()) {
456 if (const QMetaObject *targetMetaObject = targetType.metaObjectForValueType()) {
457 if (source.isObject()) {
458 doWriteProperties(targetMetaObject, defaultConstruct(), source);
459 return true;
460 }
461 if (targetType.canConstructValueType()) {
462 if (fromMatchingType(targetMetaObject, source, allocate))
463 return true;
464 warn(targetMetaObject);
465 }
466 }
467 } else if (targetType.canConstructValueType()) {
468 if (const QMetaObject *targetMetaObject = targetType.metaObjectForValueType()) {
469 if (fromMatchingType(targetMetaObject, source, allocate))
470 return true;
471 warn(targetMetaObject);
472 }
473 }
474
475 if (const auto valueTypeFunction = targetType.createValueTypeFunction()) {
476 const QVariant result
477 = valueTypeFunction(QJSValuePrivate::fromReturnedValue(source.asReturnedValue()));
478 const QMetaType resultType = result.metaType();
479 if (resultType == targetType.typeId()) {
480 resultType.construct(allocate(), result.constData());
481 return true;
482 }
483 }
484
485 return false;
486}
487
488template<typename Allocate, typename DefaultConstruct>
490 const QQmlType &targetType, QMetaType sourceMetaType, void *source,
491 Allocate &&allocate, DefaultConstruct &&defaultConstruct)
492{
493
494 const auto warn = [&](const QMetaObject *targetMetaObject) {
495 qWarning().noquote()
496 << "Could not find any constructor for value type"
497 << targetMetaObject->className() << "to call with value" << source;
498 };
499
500 if (targetType.canPopulateValueType()) {
501 if (const QMetaObject *targetMetaObject = targetType.metaObjectForValueType()) {
502 if (const QMetaObject *sourceMetaObject
503 = QQmlMetaType::metaObjectForValueType(sourceMetaType)) {
505 targetMetaObject, defaultConstruct(), sourceMetaObject,
506 [&source](const QMetaObject *sourceMetaObject, int sourceProperty) {
507 return sourceMetaObject->property(sourceProperty).readOnGadget(source);
508 });
509 return true;
510 }
511
512 if (sourceMetaType == QMetaType::fromType<QVariantMap>()) {
514 targetMetaObject, defaultConstruct(),
515 *static_cast<const QVariantMap *>(source));
516 return true;
517 }
518
519 if (sourceMetaType == QMetaType::fromType<QVariantHash>()) {
521 targetMetaObject, defaultConstruct(),
522 *static_cast<const QVariantHash *>(source));
523 return true;
524 }
525
526 if (sourceMetaType.flags() & QMetaType::PointerToQObject) {
528 targetMetaObject, defaultConstruct(),
529 *static_cast<QObject *const *>(source));
530 return true;
531 }
532 }
533 }
534
535 if (targetType.canConstructValueType()) {
536 if (const QMetaObject *targetMetaObject = targetType.metaObjectForValueType()) {
537 if (fromMatchingType(targetMetaObject, std::forward<Allocate>(allocate),
538 [&](QMetaType, auto callback) {
539 return callback(sourceMetaType, source);
540 })) {
541 return true;
542 }
543 warn(targetMetaObject);
544 }
545 }
546
547 return false;
548}
549
559 QMetaType targetMetaType, void *target, QMetaType sourceMetaType, void *source)
560{
561 if (sourceMetaType == QMetaType::fromType<QJSValue>()) {
562 const QJSValue *val = static_cast<const QJSValue *>(source);
563 return populateValueType(
565 }
566
567 if (!isConstructibleMetaType(targetMetaType))
568 return false;
569
571 QQmlMetaType::qmlType(targetMetaType), sourceMetaType, source,
572 [targetMetaType, target]() {
573 targetMetaType.destruct(target);
574 return target;
575 }, [target]() {
576 return target;
577 });
578}
579
589 QMetaType targetMetaType, void *target, const QV4::Value &source)
590{
591 if (!isConstructibleMetaType(targetMetaType))
592 return false;
593
595 QQmlMetaType::qmlType(targetMetaType), source, [targetMetaType, target]() {
596 targetMetaType.destruct(target);
597 return target;
598 }, [target]() {
599 return target;
600 });
601}
602
608 const QQmlType &targetType, const QV4::Value &source)
609{
610 void *target = nullptr;
612 targetType, source, [&]() {
613 const QMetaType metaType = targetType.typeId();
614 const ushort align = metaType.alignOf();
615 target = align > __STDCPP_DEFAULT_NEW_ALIGNMENT__
616 ? operator new(metaType.sizeOf(), std::align_val_t(align))
617 : operator new(metaType.sizeOf());
618 return target;
619 }, [&]() {
620 target = targetType.typeId().create();
621 return target;
622 })) {
623 Q_ASSERT(target != nullptr);
624 }
625
626 return target;
627}
628
630 QMetaType targetMetaType, const QMetaObject *targetMetaObject,
631 int ctorIndex, void *ctorArg)
632{
634 fromVerifiedType(targetMetaObject, ctorIndex, ctorArg,
635 [&]() { return createVariantData(targetMetaType, &result); });
636 return result;
637}
638
639static QVariant fromJSValue(const QQmlType &type, const QJSValue &s, QMetaType metaType)
640{
641 if (const auto valueTypeFunction = type.createValueTypeFunction()) {
642 const QVariant result = valueTypeFunction(s);
643 if (result.metaType() == metaType)
644 return result;
645 }
646
647 return QVariant();
648}
649
651{
652 if (!isConstructibleMetaType(metaType))
653 return QVariant();
654 return fromJSValue(QQmlMetaType::qmlType(metaType), s, metaType);
655}
656
658{
659 if (!isConstructibleMetaType(metaType))
660 return QVariant();
661 const QQmlType type = QQmlMetaType::qmlType(metaType);
662 if (type.canConstructValueType()) {
663 if (const QMetaObject *mo = type.metaObjectForValueType()) {
665 if (fromString(mo, s, [&]() { return createVariantData(metaType, &result); }))
666 return result;
667 }
668 }
669
670 return fromJSValue(type, s, metaType);
671}
672
674{
675 if (!isConstructibleMetaType(metaType))
676 return QVariant();
677 const QQmlType type = QQmlMetaType::qmlType(metaType);
678 const auto warn = [&](const QMetaObject *mo) {
679 qWarning().noquote()
680 << "Could not find any constructor for value type"
681 << mo->className() << "to call with value" << s.toQStringNoThrow();
682 };
683
684 if (type.canPopulateValueType()) {
685 if (const QMetaObject *mo = type.metaObject()) {
686 QVariant result = byProperties(mo, metaType, s);
687 if (result.isValid())
688 return result;
689 if (type.canConstructValueType()) {
690 if (fromMatchingType(mo, s, [&]() { return createVariantData(metaType, &result); }))
691 return result;
692 warn(mo);
693 }
694 }
695 } else if (type.canConstructValueType()) {
696 if (const QMetaObject *mo = type.metaObject()) {
698 if (fromMatchingType(mo, s, [&]() { return createVariantData(metaType, &result); }))
699 return result;
700 warn(mo);
701 }
702 }
703
704 return fromJSValue(type, QJSValuePrivate::fromReturnedValue(s.asReturnedValue()), metaType);
705
706}
707
713{
714 if (!isConstructibleMetaType(metaType))
715 return QVariant();
716 const QQmlType type = QQmlMetaType::qmlType(metaType);
717 const auto warn = [&](const QMetaObject *mo) {
718 qWarning().noquote()
719 << "Could not find any constructor for value type"
720 << mo->className() << "to call with value" << s;
721 };
722
723 if (type.canPopulateValueType()) {
724 if (const QMetaObject *mo = type.metaObjectForValueType()) {
725 QVariant result = byProperties(mo, metaType, s);
726 if (result.isValid())
727 return result;
728 if (type.canConstructValueType()) {
729 if (fromMatchingType(mo, s, [&]() { return createVariantData(metaType, &result); }))
730 return result;
731 warn(mo);
732 }
733 }
734 } else if (type.canConstructValueType()) {
735 if (const QMetaObject *mo = type.metaObjectForValueType()) {
737 if (fromMatchingType(mo, s, [&]() { return createVariantData(metaType, &result); }))
738 return result;
739 warn(mo);
740 }
741 }
742
743 return QVariant();
744}
745
747QVariant QQmlColorProvider::colorFromString(const QString &, bool *ok) { if (ok) *ok = false; return QVariant(); }
748unsigned QQmlColorProvider::rgbaFromString(const QString &, bool *ok) { if (ok) *ok = false; return 0; }
749QVariant QQmlColorProvider::fromRgbF(double, double, double, double) { return QVariant(); }
750QVariant QQmlColorProvider::fromHslF(double, double, double, double) { return QVariant(); }
751QVariant QQmlColorProvider::fromHsvF(double, double, double, double) { return QVariant(); }
755{
756 return QVariant();
757}
759
761
763{
765 colorProvider = newProvider;
766 return old;
767}
768
770{
771 if (colorProvider == nullptr) {
772 qWarning() << "Warning: QQml_colorProvider: no color provider has been set!";
773 static QQmlColorProvider nullColorProvider;
774 colorProvider = &nullColorProvider;
775 }
776
777 return &colorProvider;
778}
779
781{
782 static QQmlColorProvider **providerPtr = getColorProvider();
783 return *providerPtr;
784}
785
786
789{
790 return new QQmlApplication(parent);
791}
793bool QQmlGuiProvider::openUrlExternally(const QUrl &) { return false; }
794
796{
797 // We don't have any input method code by default
798 QObject *o = new QObject();
799 o->setObjectName(QStringLiteral("No inputMethod available"));
801 return o;
802}
803
805{
806 QObject *o = new QObject();
807 o->setObjectName(QStringLiteral("No styleHints available"));
809 return o;
810}
811
813
814static QQmlGuiProvider *guiProvider = nullptr;
815
817{
819 guiProvider = newProvider;
820 return old;
821}
822
824{
825 if (guiProvider == nullptr) {
826 static QQmlGuiProvider nullGuiProvider; //Still provides an application with no GUI support
827 guiProvider = &nullGuiProvider;
828 }
829
830 return &guiProvider;
831}
832
834{
835 static QQmlGuiProvider **providerPtr = getGuiProvider();
836 return *providerPtr;
837}
838
839//Docs in qqmlengine.cpp
844
846 : QObject(dd, parent)
847{
849 this, SIGNAL(aboutToQuit()));
850 connect(QCoreApplication::instance(), SIGNAL(applicationNameChanged()),
851 this, SIGNAL(nameChanged()));
852 connect(QCoreApplication::instance(), SIGNAL(applicationVersionChanged()),
853 this, SIGNAL(versionChanged()));
854 connect(QCoreApplication::instance(), SIGNAL(organizationNameChanged()),
855 this, SIGNAL(organizationChanged()));
856 connect(QCoreApplication::instance(), SIGNAL(organizationDomainChanged()),
857 this, SIGNAL(domainChanged()));
858}
859
861{
862 Q_D(QQmlApplication);
863 if (!d->argsInit) {
864 d->argsInit = true;
866 }
867 return d->args;
868}
869
871{
872 return QCoreApplication::instance()->applicationName();
873}
874
876{
877 return QCoreApplication::instance()->applicationVersion();
878}
879
881{
882 return QCoreApplication::instance()->organizationName();
883}
884
886{
887 return QCoreApplication::instance()->organizationDomain();
888}
889
891{
892 QCoreApplication::instance()->setApplicationName(arg);
893}
894
896{
897 QCoreApplication::instance()->setApplicationVersion(arg);
898}
899
901{
902 QCoreApplication::instance()->setOrganizationName(arg);
903}
904
906{
907 QCoreApplication::instance()->setOrganizationDomain(arg);
908}
909
910static const QQmlData *ddata_for_cast(QObject *object)
911{
912 Q_ASSERT(object);
913 auto ddata = QQmlData::get(object, false);
914 return (ddata && ddata->propertyCache) ? ddata : nullptr;
915}
916
918{
919 Q_ASSERT(mo);
920 if (const QQmlData *ddata = ddata_for_cast(object))
921 return ddata->propertyCache->firstCppMetaObject()->inherits(mo);
922 return object->metaObject()->inherits(mo);
923}
924
926{
927 Q_ASSERT(type.isValid());
928
929 // A non-composite type will always have a metaobject.
930 const QMetaObject *typeMetaObject = type.metaObject();
931 const QQmlPropertyCache::ConstPtr typePropertyCache = typeMetaObject
934
935 if (const QQmlData *ddata = ddata_for_cast(object)) {
936 for (const QQmlPropertyCache *propertyCache = ddata->propertyCache.data(); propertyCache;
937 propertyCache = propertyCache->parent().data()) {
938
939 if (typeMetaObject) {
940 // Prefer the metaobject inheritance mechanism, since it is more accurate.
941 //
942 // Assume the object can be casted to the type. Then, if we have a type metaobject,
943 // the object's property cache inheritance has to contain it. Otherwise we would
944 // end up with diverging metaobject hierarchies if we created the object's
945 // metaobject. This would be a disaster.
946 if (const QMetaObject *objectMetaObject = propertyCache->metaObject())
947 return objectMetaObject->inherits(typeMetaObject);
948 } else {
949 // This is a best effort attempt. There are a number of ways for the
950 // property caches to be unrelated but the types still convertible.
951 // Multiple property caches can hold the same metaobject, for example for
952 // versions of non-composite types.
953 if (propertyCache == typePropertyCache.data())
954 return true;
955 }
956 }
957 }
958
959 // If nothing else works, we have to create the metaobjects.
960
961 return object->metaObject()->inherits(typeMetaObject
962 ? typeMetaObject
963 : (typePropertyCache ? typePropertyCache->createMetaObject() : nullptr));
964}
965
967
968#include "moc_qqmlglobal_p.cpp"
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
static QStringList arguments()
static void setObjectOwnership(QObject *, ObjectOwnership)
Sets the ownership of object.
@ JavaScriptOwnership
Definition qjsengine.h:281
static QJSValue fromReturnedValue(QV4::ReturnedValue d)
Definition qjsvalue_p.h:197
static QV4::ReturnedValue asReturnedValue(const QJSValue *jsval)
Definition qjsvalue_p.h:257
The QJSValue class acts as a container for Qt/JavaScript data types.
Definition qjsvalue.h:31
QString arg(Args &&...args) const
\inmodule QtCore
Definition qmetaobject.h:19
\inmodule QtCore
bool writeOnGadget(void *gadget, const QVariant &value) const
QMetaType metaType() const
const char * name() const
Returns this property's name.
\inmodule QtCore
Definition qmetatype.h:341
constexpr TypeFlags flags() const
Definition qmetatype.h:2658
constexpr qsizetype sizeOf() const
Definition qmetatype.h:2648
constexpr qsizetype alignOf() const
Definition qmetatype.h:2653
int id(int=0) const
Definition qmetatype.h:475
@ SharedPointerToQObject
Definition qmetatype.h:408
@ WeakPointerToQObject
Definition qmetatype.h:409
@ IsUnsignedEnumeration
Definition qmetatype.h:411
@ PointerToQObject
Definition qmetatype.h:406
@ IsEnumeration
Definition qmetatype.h:407
@ TrackingPointerToQObject
Definition qmetatype.h:410
@ PointerToGadget
Definition qmetatype.h:413
void * create(const void *copy=nullptr) const
constexpr const QMetaObject * metaObject() const
Definition qmetatype.h:2663
constexpr const char * name() const
Definition qmetatype.h:2680
void * construct(void *where, const void *copy=nullptr) const
static bool convert(QMetaType fromType, const void *from, QMetaType toType, void *to)
Converts the object at from from fromType to the preallocated space at to typed toType.
friend class QVariant
Definition qmetatype.h:796
\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
QStringList args()
void setDomain(const QString &arg)
void versionChanged()
void setOrganization(const QString &arg)
void domainChanged()
void setVersion(const QString &arg)
void setName(const QString &arg)
void organizationChanged()
QQmlApplication(QObject *parent=nullptr)
virtual QVariant colorFromString(const QString &, bool *)
virtual QVariant lighter(const QVariant &, qreal)
virtual QVariant tint(const QVariant &, const QVariant &)
virtual QVariant fromRgbF(double, double, double, double)
virtual unsigned rgbaFromString(const QString &, bool *)
virtual QVariant fromHsvF(double, double, double, double)
virtual QVariant fromHslF(double, double, double, double)
virtual ~QQmlColorProvider()
virtual QVariant darker(const QVariant &, qreal)
virtual QVariant alpha(const QVariant &, qreal)
static QQmlData * get(QObjectPrivate *priv, bool create)
Definition qqmldata_p.h:199
virtual ~QQmlGuiProvider()
virtual bool openUrlExternally(const QUrl &)
virtual QStringList fontFamilies()
virtual QQmlApplication * application(QObject *parent)
virtual QObject * styleHints()
virtual QString pluginName() const
virtual QObject * inputMethod()
static const QMetaObject * metaObjectForValueType(QMetaType type)
static QQmlPropertyCache::ConstPtr findPropertyCacheInCompositeTypes(QMetaType t)
static QQmlType qmlType(const QString &qualifiedName, QTypeRevision version)
Returns the type (if any) of URI-qualified named qualifiedName and version specified by version_major...
T * data() const
const QMetaObject * metaObjectForValueType() const
Definition qqmltype.cpp:688
bool canPopulateValueType() const
Definition qqmltype.cpp:563
QMetaType typeId() const
Definition qqmltype.cpp:668
CreateValueTypeFunc createValueTypeFunction() const
Definition qqmltype.cpp:549
bool canConstructValueType() const
Definition qqmltype.cpp:556
static bool populateValueType(QMetaType targetMetaType, void *target, const QV4::Value &source)
static Q_QML_EXPORT void * heapCreateValueType(const QQmlType &targetType, const QV4::Value &source)
static QVariant constructValueType(QMetaType targetMetaType, const QMetaObject *targetMetaObject, int ctorIndex, void *ctorArg)
static QVariant createValueType(const QJSValue &, QMetaType)
const_iterator constEnd() const noexcept
Definition qset.h:143
const_iterator constFind(const T &value) const
Definition qset.h:161
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
\inmodule QtCore
Definition qurl.h:94
\inmodule QtCore
Definition qvariant.h:65
DataPtr & data_ptr()
Definition qvariant.h:710
void * data()
Returns a pointer to the contained object as a generic void* that can be written to.
bool isValid() const
Returns true if the storage type of this variant is not QMetaType::UnknownType; otherwise returns fal...
Definition qvariant.h:714
QMetaType metaType() const
Definition lalr.h:268
QSet< QString >::iterator it
auto mo
[7]
Combined button and popup list for selecting options.
Scoped< String > ScopedString
QList< QString > QStringList
Constructs a string list that contains the given string, str.
static QDBusError::ErrorType get(const char *name)
#define qWarning
Definition qlogging.h:166
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLuint GLuint end
GLuint object
[3]
GLenum type
GLenum target
GLsizei GLsizei GLchar * source
GLdouble s
[6]
Definition qopenglext.h:235
GLuint GLfloat * val
GLuint64EXT * result
[6]
GLfloat GLfloat p
[1]
static QQmlGuiProvider ** getGuiProvider(void)
Q_AUTOTEST_EXPORT QQmlGuiProvider * QQml_guiProvider(void)
static void callConstructor(const QMetaObject *targetMetaObject, int i, void *source, void *target)
static QQmlGuiProvider * guiProvider
static void fromVerifiedType(const QMetaObject *targetMetaObject, int ctorIndex, void *source, Allocate &&allocate)
static void doWriteProperties(const QMetaObject *targetMetaObject, void *target, const QV4::Value &source)
static QVariant fromJSValue(const QQmlType &type, const QJSValue &s, QMetaType metaType)
Q_QML_EXPORT QQmlGuiProvider * QQml_setGuiProvider(QQmlGuiProvider *newProvider)
static bool doWriteProperty(const QMetaProperty &metaProperty, void *target, Get &&get, Convert &&convert)
static bool fromString(const QMetaObject *mo, QString s, Allocate &&allocate)
Q_QML_EXPORT QQmlColorProvider * QQml_setColorProvider(QQmlColorProvider *newProvider)
static QQmlColorProvider * colorProvider
static QVariant byProperties(const QMetaObject *targetMetaObject, QMetaType metaType, const QV4::Value &source)
Q_AUTOTEST_EXPORT QQmlColorProvider * QQml_colorProvider(void)
static QQmlColorProvider ** getColorProvider(void)
static bool fromMatchingType(const QMetaObject *targetMetaObject, Allocate &&allocate, Retrieve &&retrieve)
bool qmlobject_can_cpp_cast(QObject *object, const QMetaObject *mo)
static const QQmlData * ddata_for_cast(QObject *object)
bool qmlobject_can_qml_cast(QObject *object, const QQmlType &type)
bool createOrConstructValueType(const QQmlType &targetType, const QV4::Value &source, Allocate &&allocate, DefaultConstruct &&defaultConstruct)
static void * createVariantData(QMetaType type, QVariant *variant)
static QT_BEGIN_NAMESPACE bool isConstructibleMetaType(const QMetaType metaType)
static constexpr To convert(const std::array< Mapping, N > &mapping, From Mapping::*from, To Mapping::*to, From value, To defaultValue)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
SSL_CTX int void * arg
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
#define Q_AUTOTEST_EXPORT
unsigned short ushort
Definition qtypes.h:33
double qreal
Definition qtypes.h:187
ReturnedValue read(const char *data)
const char property[13]
Definition qwizard.cpp:101
QVariant variant
[1]
\inmodule QtCore
int propertyCount() const
Returns the number of properties in this class, including the number of properties provided by each b...
QMetaProperty property(int index) const
Returns the meta-data for the property with the given index.
QMetaMethod constructor(int index) const
int static_metacall(Call, int, void **) const
int constructorCount() const
Heap::String * newString(char16_t c)
static QVariant toVariant(const QV4::Value &value, QMetaType typeHint, bool createJSValueForObjectsAndSymbols=true)
ExecutionEngine * engine
static PrivateShared * create(size_t size, size_t align)
Definition qvariant_p.h:61
static constexpr bool canUseInternalSpace(const QtPrivate::QMetaTypeInterface *type)
Definition qvariant.h:103