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?
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
};
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.