Signals and Slots in Qt5
Qt5 alpha has been released. One of the features which I have been working on is a new syntax for signals and slot. This blog entry will present it.
Previous syntax
Here is how you would connect a signal to a slot:
connect(sender, SIGNAL(valueChanged(QString,QString)), receiver, SLOT(updateValue(QString)) );
What really happens behind the scenes is that the SIGNAL
and SLOT
macros
will convert their argument to a string.
Then QObject::connect()
will compare those strings with the introspection data
collected by the moc tool.
What's the problem with this syntax?
While working fine in general, we can identify some issues:
- No compile time check: All the checks are done at run-time by parsing the strings. That means if you do a typo in the name of the signal or the slot, it will compile but the connection will not be made, and you will only notice a warning in the standard output.
- Since it operates on the strings, the type names of the slot must match exactly the ones of the
signal. And they also need to be the same in the header and in the connect statement.
This means it won't work nicely if you want to use
typedef
or namespaces
New syntax: using function pointers
In the upcoming Qt5, an alternative syntax exist. The former syntax will still work. But you can now also use this new way of connecting your signals to your slots:
connect(sender, &Sender::valueChanged, receiver, &Receiver::updateValue );
Which one is the more beautiful is a matter of taste. One can quickly get used to the new syntax.
So apart from the aesthetic point of view, let us go over some of the things that it brings us:
Compile-time checking
You will get a compiler error if you misspelled the signal or slot name,
or if the arguments of your slot do not match those from the signal.
This might save you some time while you are doing some re-factoring and change the
name or arguments of signals or slots.
An effort has been made, using static_assert to get nice compile errors if the arguments do not match or of you miss a Q_OBJECT
Arguments automatic type conversion
Not only you can now use typedef
or namespaces properly, but you can also connect signals
to slots that take arguments of different types if an implicit conversion is possible
In the following example, we connect a signal that has a QString
as a parameter to a slot that takes a QVariant
.
It works because QVariant
has an implicit constructor that takes a QString
class Test : public QObject { Q_OBJECT public: Test() { connect(this, &Test::someSignal, this, &Test::someSlot); } signals: void someSignal(const QString &); public: void someSlot(const QVariant &); };
Connecting to any function
As you might have seen in the previous example, the slot was just declared as public
and not as slot
. Qt will indeed call directly the function pointer of the slot, and
will not need moc
introspection anymore. (It still needs it for the signal)
But what we can also do is connecting to any function or functor:
static void someFunction() { qDebug() << "pressed"; } // ... somewhere else QObject::connect(button, &QPushButton::clicked, someFunction);
This can become very powerful when you associate that with boost or tr1::bind
.
C++11 lambda expressions
Everything documented here works with the plain old C++98. But if you use compiler that supports C++11, I really recommend you to use some of the language's new features. Lambda expressions are supported by at least MSVC 2010, GCC 4.5, clang 3.1. For the last two, you need to pass -std=c++0x as a flag.
You can then write code like:
void MyWindow::saveDocumentAs() { QFileDialog *dlg = new QFileDialog(); dlg->open(); QObject::connect(dlg, &QDialog::finished, [=](int result) { if (result) { QFile file(dlg->selectedFiles().first()); // ... save document here ... } dlg->deleteLater(); }); }
This allows you to write asynchronous code very easily.
Update: Also have a look what other C++11 features Qt5 offers.
So what now?
It is time to try it out. Check out the alpha and start playing. Don't hesistate to report bugs.
Woboq is a software company that specializes in development and consulting around Qt and C++. Hire us!
If you like this blog and want to read similar articles, consider subscribing via our RSS feed (Via Google Feedburner, Privacy Policy), by e-mail (Via Google Feedburner, Privacy Policy) or follow us on twitter or add us on G+.
Article posted by Olivier Goffart on 12 April 2012
Click to subscribe via RSS or e-mail on Google Feedburner. (external service).
Click for the privacy policy of Google Feedburner.
Google Analytics Tracking Opt-Out
Loading comments embeds an external widget from disqus.com.
Check disqus privacy policy for more information.