Solved How to add Arguments to bool QMetaObject::invokeMethod(QObject *context, Functor function, FunctorReturnType *ret)
-
bool QMetaObject::invokeMethod(QObject *context, Functor function, FunctorReturnType *ret) is an overloaded function.
I don't want to use bool QMetaObject::invokeMethod(QObject *obj, const char member, ...)
const char is not convenient when the function name is changed. -
@Toocold
What is your question? There are a number of overloads forQMetaObject::invokeMethod
accepting parameters for you to choose from. If you want to use one with aFunctor function
argument and pass parameters I think you have to use a lambda, e.g. https://forum.qt.io/topic/101182/qmetaobject-invokemethod-with-arguments. -
@JonB Thanks, it is my question.
But I wonder if I use it like this:QMetaObject::invokeMethod(obj, [=]() { obj.exec() }, Qt::BlockingQueuedConnection); Souds like this: QMetaObject::invokeMethod(NULL, [=]() { obj.exec() }, Qt::BlockingQueuedConnection);
Is it Right? And is it thread-safe?
-
@Toocold said in How to add Arguments to bool QMetaObject::invokeMethod(QObject *context, Functor function, FunctorReturnType *ret):
Is it Right?
no, the second one will also be executed even if obj is destroyed in the meantime, the first will will not even compile.
And is it thread-safe?
What exactly?
-
@Christian-Ehrlicher The obj is created in GUI Thread, and I call this QMetaObject::invokeMethod int the another thread.
QMetaObject::invokeMethod(obj, [=]() { obj.exec() }, Qt::BlockingQueuedConnection);
Will the lambda function be excuted in the GUI Thread?
-
@Toocold said in How to add Arguments to bool QMetaObject::invokeMethod(QObject *context, Functor function, FunctorReturnType *ret):
Will the lambda function be excuted in the GUI Thread?
First, your code still does not compile. Second it is executed in the thread where obj lives in since it's a QueuedConnection - it's all documented
-
@Christian-Ehrlicher Thanks. It cleared up my confusion