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