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
src_corelib_kernel_qmetaobject.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QMetaObject>
5#include <QMetaMethod>
6#include <QByteArray>
7#include <QStringView>
8
9//! [0]
10class MyClass : public QObject
11{
12 Q_OBJECT
13 Q_CLASSINFO("author", "Sabrina Schweinsteiger")
14 Q_CLASSINFO("url", "http://doc.moosesoft.co.uk/1.0/")
15
16public:
17 //...
18};
19//! [0]
20
21
22void wrapInFunction(QObject *obj, QObject *thread)
23{
24 {
25 //! [1]
26 QByteArray normType = QMetaObject::normalizedType(" int const *");
27 // normType is now "const int*"
28 //! [1]
29 }
30
31 {
32 //! [2]
33 QMetaObject::invokeMethod(thread, "quit", Qt::QueuedConnection);
34 //! [2]
35 }
36
37#if 0
38 //! [3]
39 QMetaObject::invokeMethod: Unable to handle unregistered datatype 'MyType'
40 //! [3]
41#endif
42
43 {
44 //! [invokemethod-no-macro]
45 QString retVal;
46 QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection,
47 qReturnArg(retVal),
48 QString("sqrt"), 42, 9.7);
49 //! [invokemethod-no-macro]
50 }
51
52 {
53 //! [invokemethod-no-macro-other-types]
54 QString retVal;
55 QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection,
56 qReturnArg(retVal),
57 QStringView(u"sqrt"), qsizetype(42), 9.7f);
58 //! [invokemethod-no-macro-other-types]
59 }
60
61 {
62 //! [4]
63 QString retVal;
64 QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection,
65 Q_RETURN_ARG(QString, retVal),
66 Q_ARG(QString, "sqrt"),
67 Q_ARG(int, 42),
68 Q_ARG(double, 9.7));
69 //! [4]
70 }
71}
72
73class MainWindow : public QObject
74{
76 public:
77 void show() {}
78
79 //! [10]
80 // In the class MainWindow declaration
81 #ifndef Q_MOC_RUN
82 // define the tag text as empty, so the compiler doesn't see it
83 # define MY_CUSTOM_TAG
84 #endif
85 //...
86 private slots:
88 //! [10]
89};
90
91void examples(QObject *obj, QObject *pushButton)
92{
93 {
94 //! [propertyCount]
95 const QMetaObject* metaObject = obj->metaObject();
96 QStringList properties;
97 for (int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i)
98 properties << QString::fromLatin1(metaObject->property(i).name());
99 //! [propertyCount]
100 }
101
102 {
103 //! [methodCount]
104 const QMetaObject* metaObject = obj->metaObject();
105 QStringList methods;
106 for (int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i)
107 methods << QString::fromLatin1(metaObject->method(i).methodSignature());
108 //! [methodCount]
109
110 //! [6]
111 int methodIndex = pushButton->metaObject()->indexOfMethod("animateClick()");
112 QMetaMethod method = metaObject->method(methodIndex);
113 method.invoke(pushButton, Qt::QueuedConnection);
114 //! [6]
115 }
116
117#if 0
118 //! [7]
119 QMetaMethod::invoke: Unable to handle unregistered datatype 'MyType'
120 //! [7]
121#endif
122
123 {
124 //! [invoke-no-macro]
125 QString retVal;
126 QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QString, int, double)");
127 int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature);
128 QMetaMethod method = obj->metaObject()->method(methodIndex);
129 method.invoke(obj, Qt::DirectConnection, qReturnArg(retVal),
130 QString("sqrt"), 42, 9.7);
131 //! [invoke-no-macro]
132 }
133
134 {
135 //! [invoke-no-macro-other-types]
136 QString retVal;
137 QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QByteArray, qint64, long double)");
138 int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature);
139 QMetaMethod method = obj->metaObject()->method(methodIndex);
140 method.invoke(obj, Qt::DirectConnection, qReturnArg(retVal),
141 QByteArray("sqrt"), qint64(42), 9.7L);
142 //! [invoke-no-macro-other-types]
143 }
144
145 {
146 //! [8]
147 QString retVal;
148 QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QString, int, double)");
149 int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature);
150 QMetaMethod method = obj->metaObject()->method(methodIndex);
151 method.invoke(obj,
152 Qt::DirectConnection,
153 Q_RETURN_ARG(QString, retVal),
154 Q_ARG(QString, "sqrt"),
155 Q_ARG(int, 42),
156 Q_ARG(double, 9.7));
157 //! [8]
158 }
159
160 {
161 //! [9]
162 QMetaMethod destroyedSignal = QMetaMethod::fromSignal(&QObject::destroyed);
163 //! [9]
164 }
165
166 {
167 //! [11]
168 MainWindow win;
169 win.show();
170
171 int functionIndex = win.metaObject()->indexOfSlot("testFunc()");
172 QMetaMethod mm = win.metaObject()->method(functionIndex);
173 qDebug() << mm.tag(); // prints MY_CUSTOM_TAG
174 //! [11]
175 }
176}
void wrapInFunction(QObject *obj, QObject *thread)
[0]
void examples(QObject *obj, QObject *pushButton)
#define MY_CUSTOM_TAG
[10]