Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

QT: Problem with replacing old signal slot syntax with new style in legacy code

There is a common method using old style connection which is used by many different classes (All of them directly or indirectly inherit from QObject) to subscribe for a data change

void ManagerClass::SubscribeForDataChange(QObject* item)
{
    connect(this, SIGNAL(NotifyDataChanged(unsigned int)), item, SLOT(ReceiveData(unsigned int)), Qt::AutoConnection);
}

This old style works just fine but I want to change this to new style. But now as per new style we should have the exact function signature, and ReceiveData slot is not part of QObject class. Hence converting it to new style like this is failing in compilation

connect(this, &ManagerClass::NotifyDataChanged, item, &QObject::ReceiveData, Qt::AutoConnection);

What exact changes will I need to do to make this work with new style with minimum refactoring?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I possibly think, that I will need to add a new interface(ICommonData.h) inheriting from QObject, where I will add this ReceiveData slot as pure virtual slot. Now I will implement this interface in all the classes that are subscribing with ManagerClass. Along with this I will change the signature of subscribe method to SubscribeForDataChange(ICommonData* item)

Is this understanding correct?

>Solution :

Your understanding is correct.

The most important change between the old style and new style is that in the old style, signals and slots are resolved during runtime, while in the new style, they are resolved by the compiler.

Using the interface pattern makes sense if you do have several classes that share a common interface and want to build common connections between the objects regardless of their specific implementation. It is, in fact, a strength of the new method, that you can directly express your architecture also in connect() statements.

Here is a code example where an interface class is used for connecting QObject-based objects:

  1. The base class: https://github.com/phys2/belki/blob/master/src/core/viewer.h
  2. One of the derived classes: https://github.com/phys2/belki/blob/master/src/heatmap/heatmaptab.h
  3. Factory that creates a desired object and connects base slots/signals: https://github.com/phys2/belki/blob/master/src/widgets/mainwindow.cpp#L422

(In the code example, the base class is not pure virtual. It also provides common implementation. But some of the slots are pure virtual.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading