Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlglobal_p.h
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// Qt-Security score:significant
4
5#ifndef QQMLGLOBAL_H
6#define QQMLGLOBAL_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qmetaobject_p.h>
20#include <private/qqmlmetaobject_p.h>
21#include <private/qqmltype_p.h>
22#include <private/qtqmlglobal_p.h>
23
24#include <QtQml/qqml.h>
25#include <QtCore/qobject.h>
26
28
29inline bool qmlConvertBoolConfigOption(const char *v)
30{
31 return v != nullptr && qstrcmp(v, "0") != 0 && qstrcmp(v, "false") != 0;
32}
33
34template<typename T, T(*Convert)(const char *)>
35T qmlGetConfigOption(const char *var)
36{
37 if (Q_UNLIKELY(!qEnvironmentVariableIsEmpty(var)))
38 return Convert(qgetenv(var));
39 return Convert(nullptr);
40}
41
42#define DEFINE_BOOL_CONFIG_OPTION(name, var)
43 static bool name()
44 {
45 static const bool result = qmlGetConfigOption<bool, qmlConvertBoolConfigOption>(#var);
46 return result;
47 }
48
49/*!
50 Connect \a Signal of \a Sender to \a Method of \a Receiver. \a Signal must be
51 of type \a SenderType and \a Receiver of type \a ReceiverType.
52
53 Unlike QObject::connect(), this macro caches the lookup of the signal and method
54 indexes. It also does not require lazy QMetaObjects to be built so should be
55 preferred in all QML code that might interact with QML built objects.
56
57 \code
58 QQuickTextControl *control;
59 QQuickTextEdit *textEdit;
60 qmlobject_connect(control, QQuickTextControl, SIGNAL(updateRequest(QRectF)),
61 textEdit, QQuickTextEdit, SLOT(updateDocument()));
62 \endcode
63*/
64#define qmlobject_connect(Sender, SenderType, Signal, Receiver, ReceiverType, Method) do
65 {
66 SenderType *sender = (Sender);
67 ReceiverType *receiver = (Receiver);
68 const char *signal = (Signal);
69 const char *method = (Method);
70 static int signalIdx = -1;
71 static int methodIdx = -1;
72 if (signalIdx < 0) {
73 Q_ASSERT((int(*signal) - '0') == QSIGNAL_CODE);
74 signalIdx = SenderType::staticMetaObject.indexOfSignal(signal+1);
75 }
76 if (methodIdx < 0) {
77 int code = (int(*method) - '0');
78 Q_ASSERT(code == QSLOT_CODE || code == QSIGNAL_CODE);
79 if (code == QSLOT_CODE)
80 methodIdx = ReceiverType::staticMetaObject.indexOfSlot(method+1);
81 else
82 methodIdx = ReceiverType::staticMetaObject.indexOfSignal(method+1);
83 }
84 Q_ASSERT(signalIdx != -1 && methodIdx != -1);
85 QMetaObject::connect(sender, signalIdx, receiver, methodIdx, Qt::DirectConnection); \
86}while (0)
87
88/*!
89 Disconnect \a Signal of \a Sender from \a Method of \a Receiver. \a Signal must be
90 of type \a SenderType and \a Receiver of type \a ReceiverType.
91
92 Unlike QObject::disconnect(), this macro caches the lookup of the signal and method
93 indexes. It also does not require lazy QMetaObjects to be built so should be
94 preferred in all QML code that might interact with QML built objects.
95
96 \code
97 QQuickTextControl *control;
98 QQuickTextEdit *textEdit;
99 qmlobject_disconnect(control, QQuickTextControl, SIGNAL(updateRequest(QRectF)),
100 textEdit, QQuickTextEdit, SLOT(updateDocument()));
101 \endcode
102*/
103#define qmlobject_disconnect(Sender, SenderType, Signal, Receiver, ReceiverType, Method) do
104 {
105 SenderType *sender = (Sender);
106 ReceiverType *receiver = (Receiver);
107 const char *signal = (Signal);
108 const char *method = (Method);
109 static int signalIdx = -1;
110 static int methodIdx = -1;
111 if (signalIdx < 0) {
112 Q_ASSERT((int(*signal) - '0') == QSIGNAL_CODE);
113 signalIdx = SenderType::staticMetaObject.indexOfSignal(signal+1);
114 }
115 if (methodIdx < 0) {
116 int code = (int(*method) - '0');
117 Q_ASSERT(code == QSLOT_CODE || code == QSIGNAL_CODE);
118 if (code == QSLOT_CODE)
119 methodIdx = ReceiverType::staticMetaObject.indexOfSlot(method+1);
120 else
121 methodIdx = ReceiverType::staticMetaObject.indexOfSignal(method+1);
122 }
123 Q_ASSERT(signalIdx != -1 && methodIdx != -1);
124 QMetaObject::disconnect(sender, signalIdx, receiver, methodIdx); \
125}while (0)
126
127Q_QML_EXPORT bool qmlobject_can_cpp_cast(QObject *object, const QMetaObject *mo);
128Q_QML_EXPORT bool qmlobject_can_qml_cast(QObject *object, const QQmlType &type);
129
130/*!
131 This method is identical to qobject_cast<T>() except that it does not require lazy
132 QMetaObjects to be built, so should be preferred in all QML code that might interact
133 with QML built objects.
134
135 \code
136 QObject *object;
137 if (QQuickTextEdit *textEdit = qmlobject_cast<QQuickTextEdit *>(object)) {
138 // ...Do something...
139 }
140 \endcode
141*/
142template<class T>
143T qmlobject_cast(QObject *object)
144{
145 if (!object)
146 return nullptr;
147 if (qmlobject_can_cpp_cast(object, &(std::remove_pointer_t<T>::staticMetaObject)))
148 return static_cast<T>(object);
149 else
150 return nullptr;
151}
152
153class QQuickItem;
154template<>
155inline QQuickItem *qmlobject_cast<QQuickItem *>(QObject *object)
156{
157 if (!object || !object->isQuickItemType())
158 return nullptr;
159 // QQuickItem is incomplete here -> can't use static_cast
160 // but we don't need any pointer adjustment, so reinterpret is safe
161 return reinterpret_cast<QQuickItem *>(object);
162}
163
164#define IS_SIGNAL_CONNECTED(Sender, SenderType, Name, Arguments) do
165 {
166 QObject *sender = (Sender);
167 void (SenderType::*signal)Arguments = &SenderType::Name;
168 static QMetaMethod method = QMetaMethod::fromSignal(signal);
169 static int signalIdx = QMetaObjectPrivate::signalIndex(method);
170 return QObjectPrivate::get(sender)->isSignalConnected(signalIdx); \
171}while (0)
172
173/*!
174 Makes the \a object a child of \a parent. Note that when using this method,
175 neither \a parent nor the object's previous parent (if it had one) will
176 receive ChildRemoved or ChildAdded events.
177*/
178inline void QQml_setParent_noEvent(QObject *object, QObject *parent)
179{
180 QObjectPrivate *d_ptr = QObjectPrivate::get(object);
181 bool sce = d_ptr->sendChildEvents;
182 d_ptr->sendChildEvents = false;
183 object->setParent(parent);
184 d_ptr->sendChildEvents = sce;
185}
186
188{
189public:
190 static bool populateValueType(
191 QMetaType targetMetaType, void *target, const QV4::Value &source,
192 QV4::ExecutionEngine *engine);
193 static bool populateValueType(
194 QMetaType targetMetaType, void *target, QMetaType sourceMetaType, void *source,
195 QV4::ExecutionEngine *engine);
196
197 static Q_QML_EXPORT void *heapCreateValueType(
198 const QQmlType &targetType, const QV4::Value &source, QV4::ExecutionEngine *engine);
200 QMetaType targetMetaType, const QMetaObject *targetMetaObject,
201 int ctorIndex, void **args);
202
203 static QVariant createValueType(const QJSValue &, QMetaType);
204 static QVariant createValueType(const QString &, QMetaType);
205 static QVariant createValueType(const QV4::Value &, QMetaType, QV4::ExecutionEngine *);
206 static QVariant Q_AUTOTEST_EXPORT createValueType(const QVariant &, QMetaType, QV4::ExecutionEngine *);
207};
208
209class Q_QML_EXPORT QQmlColorProvider
210{
211public:
212 virtual ~QQmlColorProvider();
213 virtual QVariant colorFromString(const QString &, bool *);
214 virtual unsigned rgbaFromString(const QString &, bool *);
215
216 virtual QVariant fromRgbF(double, double, double, double);
217 virtual QVariant fromHslF(double, double, double, double);
218 virtual QVariant fromHsvF(double, double, double, double);
219 virtual QVariant lighter(const QVariant &, qreal);
220 virtual QVariant darker(const QVariant &, qreal);
221 virtual QVariant alpha(const QVariant &, qreal);
222 virtual QVariant tint(const QVariant &, const QVariant &);
223};
224
225Q_QML_EXPORT QQmlColorProvider *QQml_setColorProvider(QQmlColorProvider *);
226Q_QML_EXPORT QQmlColorProvider *QQml_colorProvider();
227
228class QQmlApplication;
229class Q_QML_EXPORT QQmlGuiProvider
230{
231public:
232 virtual ~QQmlGuiProvider();
233 virtual QQmlApplication *application(QObject *parent);
234 virtual QObject *inputMethod();
235 virtual QObject *styleHints();
236 virtual QStringList fontFamilies();
237 virtual bool openUrlExternally(const QUrl &);
238 virtual QString pluginName() const;
239};
240
241Q_QML_EXPORT QQmlGuiProvider *QQml_setGuiProvider(QQmlGuiProvider *);
242Q_AUTOTEST_EXPORT QQmlGuiProvider *QQml_guiProvider();
243
245
246class Q_QML_EXPORT QQmlApplication : public QObject
247{
248 //Application level logic, subclassed by Qt Quick if available via QQmlGuiProvider
249 Q_OBJECT
250 Q_PROPERTY(QStringList arguments READ args CONSTANT)
251 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
252 Q_PROPERTY(QString version READ version WRITE setVersion NOTIFY versionChanged)
253 Q_PROPERTY(QString organization READ organization WRITE setOrganization NOTIFY organizationChanged)
254 Q_PROPERTY(QString domain READ domain WRITE setDomain NOTIFY domainChanged)
255 QML_ANONYMOUS
256public:
257 QQmlApplication(QObject* parent=nullptr);
258
259 QStringList args();
260
261 QString name() const;
262 QString version() const;
263 QString organization() const;
264 QString domain() const;
265
266public Q_SLOTS:
267 void setName(const QString &arg);
268 void setVersion(const QString &arg);
269 void setOrganization(const QString &arg);
270 void setDomain(const QString &arg);
271
272Q_SIGNALS:
273 void aboutToQuit();
274
275 void nameChanged();
276 void versionChanged();
277 void organizationChanged();
278 void domainChanged();
279
280protected:
281 QQmlApplication(QQmlApplicationPrivate &dd, QObject* parent=nullptr);
282
283private:
284 Q_DISABLE_COPY(QQmlApplication)
285 Q_DECLARE_PRIVATE(QQmlApplication)
286};
287
289{
290 Q_DECLARE_PUBLIC(QQmlApplication)
291public:
293 argsInit = false;
294 }
295
298};
299
301{
303 QQmlSourceLocation(const QString &sourceFile, quint16 line, quint16 column)
308};
309
310QT_END_NAMESPACE
311
312#endif // QQMLGLOBAL_H
\inmodule QtQml
QQmlPropertyObserver(QQmlBoundSignalExpression *expr)
static bool populateValueType(QMetaType targetMetaType, void *target, const QV4::Value &source, QV4::ExecutionEngine *engine)
static QVariant createValueType(const QV4::Value &, QMetaType, QV4::ExecutionEngine *)
static Q_QML_EXPORT void * heapCreateValueType(const QQmlType &targetType, const QV4::Value &source, QV4::ExecutionEngine *engine)
static bool populateValueType(QMetaType targetMetaType, void *target, QMetaType sourceMetaType, void *source, QV4::ExecutionEngine *engine)
static QVariant constructValueType(QMetaType targetMetaType, const QMetaObject *targetMetaObject, int ctorIndex, void **args)
static QVariant createValueType(const QJSValue &, QMetaType)
Combined button and popup list for selecting options.
void QQmlBoundSignal_callback(QQmlNotifierEndpoint *e, void **a)
void QQml_setParent_noEvent(QObject *object, QObject *parent)
Makes the object a child of parent.
T qmlobject_cast(QObject *object)
This method is identical to qobject_cast<T>() except that it does not require lazy QMetaObjects to be...
T qmlGetConfigOption(const char *var)
Q_QML_EXPORT bool qmlobject_can_cpp_cast(QObject *object, const QMetaObject *mo)
QT_BEGIN_NAMESPACE bool qmlConvertBoolConfigOption(const char *v)
Q_QML_EXPORT bool qmlobject_can_qml_cast(QObject *object, const QQmlType &type)
QQmlSourceLocation(const QString &sourceFile, quint16 line, quint16 column)