Besides QMetaObject::invokeMethod is there any type-safe way of invoking a method/slot asynchronously (a.k.a queuing its execution in the GUI thread)?
The QMetaObject::invokeMethod doesn't have compile-time checking for function names. There is also an overhead in specifying the member function by a string since a lookup and string matching is performed for every call.
I wonder if there is anything similar to the new QObject::connect syntax for invoking a method that provides compile time checking.
One solution is using the signal-slot architecture, but this enforces wrapping each caller code in a QObject class to be able to emit that signal.
It is possible to use the QTimer::singleShot for this purpose
QTimer::singleShot(0, object, &Object::method...);
--
QTimer::singleShot(0, object, [object](){
object->method(arg1, arg2, ...);
});
Note: The thread in which QTimer::singleShot is invoked must have an QEventLoop.
Please see this https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod-4
template <typename Functor, typename FunctorReturnType> bool QMetaObject::invokeMethod(QObject *context, Functor function, FunctorReturnType *ret)
This is an overloaded function.
Invokes the function in the event loop of context using the connection type Qt::AutoConnection. function can be a functor or a pointer to a member function. Returns true if the function could be invoked. Returns false if there is no such member or the parameters did not match. The return value of the function call is placed in ret.
Note: This function is thread-safe.
This function was introduced in Qt 5.10.
Related
In Qt, I often use something like QMetaObject::invokeMethod(this, "myMethod", Q_ARG(bool, foo)) (effectively, this causes the method call + args to be queued and later executed on the correct event queue).
Is there a way to use method references instead of using the method name as string (i.e. use &MyClass::myMethod instead of "myMethod"), while still queueing execution?
Since Qt 5.10 there are two QMetaObject::invokeMethod overloads which accept functor or a pointer to a member function as an argument.
Inside the same thread you can use QTimer::singleShot overload accepting functor (since Qt 5.4) and use zero milliseconds timeout.
I'm using Qt 5.9.2 with Visual Studio 2015 and QtDesigner for programming a Windows GUI application. I tried connecting one of my actions via the following call:
connect(ui.myAction, &QAction::triggered, memberPtrToObjX_, &ClassX::Run);
However ClassX::Run is not always triggered after clicking on myAction in the menubar. Investigating into this problem, I figured, that the same signal-slot connection using lambda syntax works:
connect(ui.myAction, &QAction::triggered, [this](bool run) { memberPtrToObjX_->Run(run); });
I'm pretty sure, that both calls are syntactically correct. Besides both calls return a valid QMetaObject::Connection, if I save the return value and check with operator bool().
Obviously I could just stick with the working lambda-version, but I'm confused and would prefer knowing the reason behind my "solution". Is there any functional difference between these two calls, that explains the different behaviour?
The two calls of QObject::connect() (exposed by the OP) behave differently in the case that this->memberPtrToObjX_ is modified after the call of connect().
The first
connect(ui.myAction, &QAction::triggered, memberPtrToObjX_, &ClassX::Run);
calls
QMetaObject::Connection QObject::connect( const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection).
Creates a connection of the given type from the signal in the sender object to the method in the receiver object. Returns a handle to the connection that can be used to disconnect it later.
Hence, the current pointer in this->memberPtrToObjX_ is connected as signal receiver. If this->memberPtrToObjX_ is modified after connect() this doesn't have any effect to the signal connection.
The second
connect(ui.myAction, &QAction::triggered, [this](bool run) { memberPtrToObjX_->Run(run); });
calls
QMetaObject::Connection QObject::connect( const QObject *sender, PointerToMemberFunction signal, Functor functor).
Creates a connection from signal in sender object to functor, and returns a handle to the connection.
Hence, the (functor behind the) lambda is connected as receiver. The lambda resolves the pointer in this->memberPtrToObjX_ at the time it is executed i.e. when the signal is triggered.
The second difference (which originally was uncovered in the comment of G.M.) is the connection type:
The first version uses the default value Qt::AutoConnection (as it is not defined explicitly). The version with the lambda uses always Qt::DirectConnection instead.
The difference appears if the pointee in this->memberPtrToObjX_ does not "live" in the same thread. In this case, the Qt::AutoConnection is resolved to Qt::QueuedConnection instead of Qt::DirectConnection.
I assumed that the pointee in this->memberPtrToObjX_ would "live" in the same QThread. If not, the second version (with the lambda) becomes very questionable as it calls the a member function of the object "living" in a different thread (where it is hard to tell what that thread is doing at this time). This only seems to work better but is very possibly a "time bomb".
A function: void function(ObjectName obj, Signal), ObjectName is a class type and obj is an instance. The question is that can the second parameter be a signal which defined in class ObjectName. That means can I use the signal as a normal member function pointer?
Of course! Think about it -- QObject::connect() is a function that takes both signals and slots as parameters. Check out its documentation for the various ways this can be implemented.
When we run
(.someMethod obj arg1 arg2 arg3)
is the clojure.lang.Reflector class used all the time and how does that affect performance?
That depends on whether the compiler can statically determine which class or interface someMethod belongs to.
If it can, then it will emit a direct call and c.l.Reflector will not be used at runtime. The result is equivalent to
((Foo) obj).someMethod(arg1, arg2, arg3);
in Java, assuming someMethod has been declared by the class / interface Foo.
If it cannot, then it will emit a call to the static method clojure.lang.Reflector.invokeInstanceMethod. In Clojure notation, the emitted call could be written
(clojure.lang.Reflector/invokeInstanceMethod
obj "someMethod" (object-array [arg1 arg2 arg3]))
This looks up the matching method through the use of reflection at runtime. The slowdown relative to a statically resolved call is quite spectacular.
Ok this is odd. It's the first time I've seen such a line of code.
Basically this calls the entry point into an application once you've specified an offset (address) from a program's PE header.
As you can tell - I've been playing lately with writing my own PE loader. I'm still a beginner and attempting to understand the structure - but what exactly is that function call mean?
((void(*)(void))EntryPoint)();
//where 0x4484502 is gotten from:
PIMAGE_NT_HEADERS nt_header;
DWORD EntryPoint = nt_header->OptionalHeader.ImageBase + nt_header->OptionalHeader.AddressOfEntryPoint;
((void(*)(void))0x4484502)();
The line
((void(*)(void))0x4484502)();
Casts the integer 0x4484502 to a point to a function (starting at that address) that has void parameters and returns void. Once cast, the function pointer is called.
EDIT:
Just re-read the question.... replace 0x4484502 with EntryPoint does exactly the same thing... the variable EntryPoint is cast as a pointer to a function that has void params and returns void. Pointer then used to call function.
notation
(some_type)something
it is C-style cast. It is equal to a sequence of C++ casts, but without dynamic_cast so it is dangerous - it allows you to cast a pointer to private base to a pointer to derived class not only in derived class functions.
here we have
(void(*)(void))0x4484502
it means that 0x4484502 is casted to a pointer to a function that takes void and returns void.
the notation func_ptr()
means call the function pointed to by func_ptr.
you can always check such strange declarations on cdecl