Calling method with array or arguments using QueuedConnection
I 开发者_如何学运维want to call arbitrary slot of QObject in other thread.
I have:
| Arguments: | Can use QueuedConnection?
QMetaObject::invokeMethod | fixed number | YES
qt_metacall | array | NO
I want:
<something> | array | YES
I don't want to do things like duplicating invokeMethod code based on the number of arguments.
Where to get invokeMethod that accepts array of arguments or how to make qt_metacall queued?
You can either:
- write a signal with the same default parameters as the slot you want to call, connect it to the slot with
Qt::QueuedConnection
and call the signal withqt_metacall
and your array, or - write a
QObject
derived class that:- takes your parameter array as parameter for its constructor, and stores it internally,
- calls
QMetaObject::invokeMethod
in the constructor withQt::QueuedConnection
to invoke a slot without parameter which will callqt_metacall
with the stored parameter array before deleting theQObject
.
Internally Qt uses the 2nd method but with a internal class: QMetaCallEvent
(in corelib/kernel/qobject_p.h) and postEvent
instead of a signal/slot connection.
Working around by creating array initialized by GenericArgument:
QGenericArgument args[] = {
QGenericArgument(), ....... ,QGenericArgument(),};
for (int p = 0; p < parameterTypes.count(); ++p) {
QVariant::Type type = QVariant::nameToType(parameterTypes.at(p));
switch(type) {
case QVariant::String:
args[p] = Q_ARG(QString, obtainTheNextStringArgument());
break;
// the rest needed types here
}
}
mm.invoke(object, Qt::QueuedConnection, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8],args[9]);
精彩评论