Skip to content

Commit ef6345d

Browse files
committed
QMetaMethod: Add QMetaMethod::from(&MyClass:myFunction) support
Add QMetaMethod::from QMetaMethod::fromInvokable QMetaMethod::fromSlot Closes: https://bugreports.qt.io/browse/QTBUG-36861 Signed-off-by: Yonggang Luo <[email protected]>
1 parent 8d6b274 commit ef6345d

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

src/corelib/kernel/qmetaobject.h

+39-5
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,48 @@ class Q_CORE_EXPORT QMetaMethod
138138
template <typename PointerToMemberFunction>
139139
static inline QMetaMethod fromSignal(PointerToMemberFunction signal)
140140
{
141-
typedef QtPrivate::FunctionPointer<PointerToMemberFunction> SignalType;
142-
static_assert(QtPrivate::HasQ_OBJECT_Macro<typename SignalType::Object>::Value,
143-
"No Q_OBJECT in the class with the signal");
144-
return fromSignalImpl(&SignalType::Object::staticMetaObject,
145-
reinterpret_cast<void **>(&signal));
141+
QMetaMethod method = from(signal);
142+
if (method.methodType() == Signal)
143+
{
144+
return method;
145+
}
146+
return QMetaMethod();
147+
}
148+
149+
template <typename PointerToMemberFunction>
150+
static inline QMetaMethod fromSlot(PointerToMemberFunction slot)
151+
{
152+
QMetaMethod method = from(slot);
153+
if (method.methodType() == Slot)
154+
{
155+
return method;
156+
}
157+
return QMetaMethod();
158+
}
159+
160+
template <typename PointerToMemberFunction>
161+
static inline QMetaMethod fromInvokable(PointerToMemberFunction invokable)
162+
{
163+
QMetaMethod method = from(invokable);
164+
if (method.methodType() == Method)
165+
{
166+
return method;
167+
}
168+
return QMetaMethod();
169+
}
170+
171+
template <typename PointerToMemberFunction>
172+
static inline QMetaMethod from(PointerToMemberFunction func)
173+
{
174+
typedef QtPrivate::FunctionPointer<PointerToMemberFunction> ClassType;
175+
static_assert(QtPrivate::HasQ_OBJECT_Macro<typename ClassType::Object>::Value,
176+
"No Q_OBJECT in the class with the func");
177+
return fromSignalImpl(&ClassType::Object::staticMetaObject,
178+
reinterpret_cast<void **>(&func));
146179
}
147180

148181
private:
182+
// ### Qt 7: rename fromSignalImpl to fromImpl
149183
static QMetaMethod fromSignalImpl(const QMetaObject *, void **);
150184
static QMetaMethod fromRelativeMethodIndex(const QMetaObject *mobj, int index);
151185
static QMetaMethod fromRelativeConstructorIndex(const QMetaObject *mobj, int index);

src/tools/moc/generator.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1157,13 +1157,13 @@ void Generator::generateStaticMetacall()
11571157
}
11581158

11591159
}
1160-
if (!cdef->signalList.isEmpty()) {
1161-
Q_ASSERT(needElse); // if there is signal, there was method.
1160+
if (!methodList.isEmpty()) {
1161+
Q_ASSERT(needElse); // there was method.
11621162
fprintf(out, " else if (_c == QMetaObject::IndexOfMethod) {\n");
11631163
fprintf(out, " int *result = reinterpret_cast<int *>(_a[0]);\n");
11641164
bool anythingUsed = false;
1165-
for (int methodindex = 0; methodindex < cdef->signalList.size(); ++methodindex) {
1166-
const FunctionDef &f = cdef->signalList.at(methodindex);
1165+
for (int methodindex = 0; methodindex < methodList.size(); ++methodindex) {
1166+
const FunctionDef &f = methodList.at(methodindex);
11671167
if (f.wasCloned || !f.inPrivateClass.isEmpty() || f.isStatic)
11681168
continue;
11691169
anythingUsed = true;

0 commit comments

Comments
 (0)