SIGNAL & SLOT macros in Qt: what do they do?
I am new to Qt and am trying to understand macros SIGNAL
and SLOT
. When I learn to use the method connect
to bind signal and slot, I found that the manuals on the official Qt man page use:
connect(obj1, SIGNAL(signal(int)), obj2, SLOT(slot()))
However, this also works very well:
connect(obj1, &Obj1::signal, obj2, &Obj2::slot)
So what do macros SIGNAL
and SLOT
do? Do they just look for a signal in the class the object belongs to and return its address?
Then why do most programmers use these macros instead of using them &Obj1::signal
, since the latter looks simpler and you don't need to change the code if the signal function parameters change?
source to share
The use of macros SIGNAL
, and SLOT
was the only way to create connections Qt 5. The connection is made at run time and requires that the title were tagged signal and slots. For example:
Class MyClass : public QObject
{
Q_OBJECT
signals:
void Signal();
slots:
void ASlotFunction();
};
To avoid repetition, the way it works is described in the QT 4 documentation .
The signal and slot mechanism is part of the C ++ extensions provided by Qt and uses the Meta Object Compiler (moc) .
This explains why signals and slots use moc.
The second connection method is greatly improved because the specified functions can be checked at compile time rather than at run time. Also, using the function address, you can refer to any function of the class, not just the sections marked with slots:
The documentation has been updated for Qt 5 .
Also, there is a nice blog post about connecting Qt 4 here and Qt 5 here .
source to share
Supplement to the first answer.
what exactly did the SIGNAL and SLOT macros do
Almost nothing. Take a look at qobjectdefs.h
:
# define SLOT(a) "1"#a
# define SIGNAL(a) "2"#a
He just adds 1
or 2
. This means that the following code is valid and works as expected:
QObject *obj = new QObject;
connect(obj,"2objectNameChanged(QString)",this,"1show()");//suppose this is a pointer to a QDialog subclass
obj->setObjectName("newNAme");
why most programmers use these macros instead of like & amp; Obj1 :: signal
- Because these macros don't only work in Qt5.
- Because with these macros the complexity with overloaded signals ( it can make your code very messy and it's really not that easy).
- Because with the new syntax, you sometimes need to use certain detaches
source to share
To complete TheDarkKnight answer , it's a great practice to refactor legacy code that uses the old QT 4 SIGNAL and SLOT macros for the new Qt 5 syntax using a function address.
Suddenly the connection error will appear at compile time, not at runtime! It is very easy to make a Qt 4 connection error, as any spelling mistake will result in such an error. In addition, the function name must be a fully qualified name, preceded by a fully qualified namespace, if any.
Another advantage is the ability to use a lambda for the slot function, which can reduce the need for the named function if the slot body is trivial.
source to share