Qt5 Slots Connect

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.

Here is how you would connect a signal to a slot:

If i create a class from a base class with virtual slots, the slots never get called with the new connect-flavour. If i use the old connect-syntax, the slot gets called. What could be the problem? @ class BaseClass: public QObject public slots: virt. When editing a connect the NEW Qt5 Style, the autocompletion does not limit the signal element to reals signals. Any member method is proposed. Not just signals. The same holds for slots. This is not helpful for the novice user. This all creates a DLL called syncro.dll, which builds, loads and runs without problem.

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?

Qt5

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

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:

  • Comparing mem and time required to create N objects each with 1 connection, N from 100 to 10000000 Measuring for 100 connections # connects mem (bytes) time (sec) Raw: 100 0 0.0015 Pyqt Slot: 100 0 0.000495 Ratios: nan 3 Measuring for 1000 connections # connects mem (bytes) time (sec) Raw: 1000 0 0.0138 Pyqt Slot: 1000 0 0.0035 Ratios.
  • Events are generated by users interacting with widgets in an application. These events cause signals to be emitted. Corresponding slots, or functions then run. Qt 5 Signals and Slots Demonstration. The following image shows the application built in this section using Qt Creator. It demonstrates some methods of using signals and slots.
  • Signals and slots are loosely coupled: A class which emits a signal neither knows nor cares which slots receive the signal. Qt's signals and slots mechanism ensures that if you connect a signal to a slot, the slot will be called with the signal's parameters at the right time. Signals and slots can take any number of arguments of any type.

Which one is the more beautiful is a matter of taste. One can quickly get used to the new syntax.

Qt5 Slots Connect Games

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 signalsto 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

Connecting to any function

As you might have seen in the previous example, the slot was just declared as publicand not as slot. Qt will indeed call directly the function pointer of the slot, andwill 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:

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 supportsC++11, I really recommend you to use some of the language's new features.Lambda expressions are supportedby at least MSVC 2010, GCC 4.5, clang 3.1. For the last two, you need to pass -std=c++0x asa flag.

You can then write code like:

This allows you to write asynchronous code very easily.

Update: Also have a look what other C++11 features Qt5 offers.

It is time to try it out. Check out the alpha and start playing. Don't hesistate to report bugs.

Quite a frequent problem when working with signals with slots in Qt5, according to my observations on the forum, is the connection of slots in the syntax on the pointers to signals having an overload of the signature. The same applies to slots that have an overload.

Let's take a test class that has overloaded signals.

Here there is a signal, with an overload of the signature. Connect this signal will also be to the slots that are declared in the Widget class, and which also have an overload of the signature.

Slots

Qt5 Slots Connect Online

How it was in Qt4

Qt5 Slots Connect

Within Qt4, everything was solved quite simply by specifying the signature of the signal and the slot in the SIGNAL and SLOT macros.

How it became in Qt5

But in Qt5, when writing in the new syntax of signals and slots, there are some problems. Because you need to make the static_cast of the method signature.

By the way, the new syntax also allows you to connect signals to slots with a smaller signature, as it was in Qt4.

Advantages of the new syntax

And now a stumbling block. Why use the new syntax of signals and slots? I still hear this question from time to time. Especially when people see such terrible castes of signatures.

Qt5 Slots Connection

  1. Therefore, I will list potential advantages:The ability to track errors in the connection of signals and slots at the compilation stage, rather than in the runtime
  2. Reducing compilation time by excluding macros from the code
  3. The ability to connect lambda functions, it's quite an important bun
  4. We protect ourselves from errors when we try to connect from the outside to a private slot. Yes!! Yes!! The SIGNAL and SLOT macros ignore the access levels of methods, violating OOP.

In general, for me this is enough, but for you?