scdmb scdmb - 11 months ago 65
C++ Question

What is the difference between a slot and a method in Qt?

What is the difference between a slot (a method declared in

section) and a method in Qt (a method declared with Q_INVOKABLE keyword)? They both can be invoked using
, they are both accepted when connecting to a slot using
macro, however when getting type of metamethod it can be returned either
, so it seems that there is some difference for Qt?

Answer Source

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

#include <QObject>
struct Test : public QObject {
  Q_SLOT void slot() {}
  Q_INVOKABLE void invokable() {}

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()));
#include "main.moc"

It's up to the user to decide how the difference between a slot and an invokable is interpreted. E.g. if you're exposing the slot list to the user in some way, you won't be exposing the invokable method list unless you choose to do so.