Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple inheritance of QObject

I have a class that would listen via connect to some signals in a couple of different contexts, and a dialog that would do said listening among other things.

class MyListener : public QObject
{
    Q_OBJECT
};

class MyDialog : public QDialog, public MyListener
{
    Q_OBJECT
};

That caused the following compilation error:

error: reference to 'connect' is ambiguous

I suspected that may be caused by multiple inheritance of QObject by MyDialog, once via QDialog and once via MyListener. However, making all the above inheritance statements virtual didn't eliminate the error.

Could you suggest what may be the cause of this?

like image 978
Michael Avatar asked Mar 26 '26 23:03

Michael


2 Answers

Make it:

class MyDialog : public QDialog
{
    Q_OBJECT

    public:
    MyListener& listener() { return m_listener; }

    private:
    MyListener m_listener;
};

Have you considered inheriting your QObject as protected instead? This is because both classes use the connect() function to connect slots and signals together in your .ui file, having each class inherit each other means you now have two possible connect functions whenever the program makes a call to connect your signal/slots

have your needed functions under protected and prevent the ambiguity of two connect()

class MyListener
{
    public:
        //...
    protected:
        int a;
        //stuff to share
};

class MyDialog: public QDialog, protected MyListener 
{

    //has access to all protected members but not the private members
};
like image 34
Syntactic Fructose Avatar answered Mar 28 '26 12:03

Syntactic Fructose