Solved QMetaObject::invokeMethod() with arguments
-
Hi,
I would like to use the Functor overload of the invokeMethod() functions since it does compile time checks that the function actually exists. However, how do I pass arguments using this method? Is it possible?
-
@btse
simply capture them in your lambda function?
You would anyway specify them at the time you call invokeMethodQMetaObject::invokeMethod(obj, [var1,var2]() { ... });
-
@raven-worx Thanks. I originally wanted to call a member function, but I guess I can wrap that call with a lambda function.
Note that
dest
lives in a separate thread. Should this be thread safe?connect( lineedit, &QLineEdit::returnPressed, [=]() { QString text = lineedit->text(); QMetaObject::invokeMethod(dest, [=]() { dest->setOutputFilePath(text); }, Qt::QueuedConnection ); } );
-
@btse said in QMetaObject::invokeMethod() with arguments:
Note that dest lives in a separate thread. Should this be thread safe?
since you explicitly use QueuedConnection yes. You can also use AutoConnection (the default anyway) to let Qt decide.
-
@raven-worx Yeah I guess the whole lambda capturing is something I still need to get comfortable with, especially when it's used in a multi-thread context.