3

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?

1

3 Answers 3

4

Make it:

class MyDialog : public QDialog
{
    Q_OBJECT

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

    private:
    MyListener m_listener;
};
2

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
};
1

making all the above inheritance statements virtual didn't eliminate the error.

This is expected, as the virtual keyword only affects the immediate base class. That is, MyListener still inherits QObject non-virtually. You cannot really change that.

On top of that, the documentation says

Virtual inheritance with QObject is not supported.

The only reasonable solution is to have MyListener not inherit QObject.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.