connect syntax
-
Is there any functional difference between using this connect syntax...
connect(&m_fileWatcher, &QFileSystemWatcher::directoryChanged, this, &MyClass::on_directoryChanged);
and this syntax...
connect(&m_fileWatcher, SIGNAL(directoryChanged(const QString&)), SLOT(on_directoryChanged(const QString&));
?
I've seen samples online using both. To me the former is a lot cleaner, especially for slots/signals with a lot of parameters, but I want to make sure there are no issues using that syntax.
Is either one preferred over the other? In my limited testing they seem to work identically, but I'm new to Qt so I want to be sure
-
-
Hi
Yes The first syntax catches errors at compile time ( called the new syntax hereafter)
The SLOT macros will eat anything at compile andsilentlyfail at runtime.The new syntax also allows to use c++ lambdas and basically anything callable as a slot.
For the compile check alone, the new syntax is preferred. BUt being able to use lambdas also is a huge plus for some code.
Its explained here
https://wiki.qt.io/New_Signal_Slot_Syntax -
@Dan203
Hi
The only real downside to the "new syntax" is function overload.
Like for some Widgets that have the same signal with different parameters, where the syntax can be somewhat
awkward.like
https://doc.qt.io/qt-5/qcombobox.html#activatedBut if you have ever used SLOT/SIGNAL macros in a larger project and have features break due to someone
renamed a slot or signal, you will want the new syntax. :) -
@mrjj said in connect syntax:
The SLOT macros will eat anything at compile and silently fail at runtime.
Failures for invalid or non-matching signature strings are detected at runtime. They're not "silent". There are several qWarnings that may be triggered, based on the specifics of the failure. The returned QMetaObject::Connection object's operator bool() should return false.
QObject::connect using functors that successfully compile may also fail at runtime, and can be detected in the same way.