Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect QT Signals declared in interface

Is there any way to use Qt5 style Signal & Slot connection if the signals are declared in interfaces ?.

My Interfaces:

class IMyInterfaces{
protected:
    IMyInterfaces() {} //Prohibit instantiate interfaces
public:
    virtual ~IMyInterfaces(){} 

signals:
    virtual void notify_signal() =0;
};
Q_DECLARE_INTERFACE(IMyInterfaces, "IMyInterfaces");

And a class which implements above interfaces:

class MyClass : public QObject, public IMyInterfaces{
    Q_OBJECT
    Q_INTERFACES(IMyInterfaces) //Indicates interface implements
public:
    MyClass():QObject(){
    }
    ~MyClass(){}

signals:
     void notify_signal();
};

In main program I would like to do something like this:

IMyInterfaces * myObject = new MyClass();
//Connect signal using Qt5 style (This will introduce compilation errors)
connect(myObject ,&IMyInterfaces::notify_signal, otherObject, &OtherClass::aSlot);

The old style works but requires to cast to QObject:

QObject::connect(dynamic_cast<QObject *>(m),SIGNAL(notify_signal()),other,SLOT(aSlot())); //This works but need to cast to QObject. 
like image 579
sng Avatar asked Sep 30 '15 22:09

sng


1 Answers

You can declare your signal in the interface as non-virtual. Since the signal won't be implemented by any class that fulfils the interface anyway this will be fine. For example:

Your interface could look like this

#include <QObject>

class IMyInterfaces : public QObject
{
    Q_OBJECT
public:
    virtual ~IMyInterfaces() {}
    virtual void TestNotify() = 0;

signals:
    void notify_signal();
};

Your class that implements the interface would look like this:

Header

#include "imyinterfaces.h"

class MyClass : public IMyInterfaces
{
    Q_OBJECT
public:
    MyClass();
    ~MyClass();
    void TestNotify();
};

Source

#include "myclass.h"

MyClass::MyClass()
{
    // Constructor stuff placed here etc.
}

MyClass::~MyClass()
{
    // Cleanup placed here etc.
}

void MyClass::TestNotify()
{
    emit notify_signal(); // This signal has been inherited from the interface
}

You would then set up the notify signal as so:

IMyInterfaces *myObject = new MyClass();
connect(myObject, &IMyInterfaces::notify_signal, this, &MainWindow::notify_signal);

myObject->TestNotify();

It sounds like the answer from Aurel Branzeanu suggests this implementation but I thought it would help to see an example.

like image 176
Ben Avatar answered Oct 26 '22 23:10

Ben