What is the difference between slot and method in Qt?

What is the difference between a slot (a method declared in the slots section) and a method in Qt (a method declared with the Q_INVOKABLE keyword)? Both of them can be called using QMetaObject::invokeMethod , they are both accepted when connecting to the slot using the SLOT macro, however when receiving the type of metamethod it can be returned either QMetaMethod::Method or QMetaMethod::Slot , so it seems that there is some difference for Qt?

+5
source share
1 answer

The only difference is whether the method is listed as a slot or as a non-slot in the class metadata. In both Qt 4 and Qt 5, the connection to the slot or invokable succeeds:

 #include <QObject> struct Test : public QObject { Q_SLOT void slot() {} Q_INVOKABLE void invokable() {} Q_OBJECT }; int main() { Test test; auto c1 = QObject::connect(&test, SIGNAL(destroyed(QObject*)), &test, SLOT(slot())); auto c2 = QObject::connect(&test, SIGNAL(destroyed(QObject*)), &test, SLOT(invokable())); Q_ASSERT(c1); Q_ASSERT(c2); } #include "main.moc" 

The user must decide how to interpret the difference between the slot and invokable. For instance. if you expand the list of slots to the user in any way, you will not expand the invokable method list unless you do so.

+10
source

All Articles