Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Dan203 last edited by Dan203
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
gde23 last edited by
Yes The first syntax catches errors at compile time ( called the new syntax hereafter)
The SLOT macros will eat anything at compile and
silentlyfail 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
Dan203 last edited by
I'm using Widgets not QML and I'd rather have a compile time error than a run time error, so it seems like the "Functor-Based Connections" are the best option for me.
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
But 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. :)
jeremy_k last edited by
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.
Yes you are right, if you are running the app in creator and/or can show
such warnings. I adjusted "silent" in above post.
In any case, the new syntax is recommended.