Both QPushButton & QLineEdit are derived from QWidget. But I'm unable to inherit both on to a single class.
When I replicate the same kinda things using generic C++, it works. I'm unable to understand why this doesn't work the same way for Qt classes ?
// A - Base class
// P, Q - Derived classes from A
// Z - Derived from P, Q
// Works fine with this order.
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A::Ctor\n"; }
~A() { cout << "A::Dtor\n"; }
};
class P : public A
{
public:
P() { cout << "P::Ctor\n"; }
~P() { cout << "P::Dtor\n"; }
};
class Q : public A
{
public:
Q() { cout << "Q::Ctor\n"; }
~Q() { cout << "Q::Dtor\n"; }
};
class Z : public P, public Q
{
public:
Z() { cout << "Z::Ctor\n"; }
~Z() { cout << "Z::Dtor\n"; }
};
int main(int argc, char *argv[])
{
Z obj;
return 0;
}
// A - Base class
// P, Q - Derived classes from A
// Z - Derived from P, Q
// Works fine with this order.
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A::Ctor\n"; }
~A() { cout << "A::Dtor\n"; }
};
class P : public A
{
public:
P() { cout << "P::Ctor\n"; }
~P() { cout << "P::Dtor\n"; }
};
class Q : public A
{
public:
Q() { cout << "Q::Ctor\n"; }
~Q() { cout << "Q::Dtor\n"; }
};
class Z : public P, public Q
{
public:
Z() { cout << "Z::Ctor\n"; }
~Z() { cout << "Z::Dtor\n"; }
};
int main(int argc, char *argv[])
{
Z obj;
return 0;
}
To copy to clipboard, switch view to plain text mode
But the same kinda inheritance isn't working for Qt classes
#include <QApplication>
#include <QPushButton>
#include <QLineEdit>
#include <QDebug>
class SomeWidget
: public QPushButton,
public QLineEdit // Both derived from QWidget, which is derived from QObject{
Q_OBJECT
public:
SomeWidget() { qDebug() << "SomeWidget::Ctor"; }
~SomeWidget() { qDebug() << "SomeWidget::Dtor"; }
};
int main(int argc, char *argv[])
{
SomeWidget wid;
wid.show();
return a.exec();
}
#include "main.moc"
#include <QApplication>
#include <QPushButton>
#include <QLineEdit>
#include <QDebug>
class SomeWidget : public QPushButton, public QLineEdit // Both derived from QWidget, which is derived from QObject
{
Q_OBJECT
public:
SomeWidget() { qDebug() << "SomeWidget::Ctor"; }
~SomeWidget() { qDebug() << "SomeWidget::Dtor"; }
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SomeWidget wid;
wid.show();
return a.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Issues I got when I compiled this
Debug/main.moc:-1: In member function 'virtual const QMetaObject* SomeWidget::metaObject() const':
error: 'QObject' is an ambiguous base of 'SomeWidget'
error: 'QObject' is an ambiguous base of 'SomeWidget'
Warning: Class SomeWidget inherits from two QObject subclasses QPushButton and QLineEdit. This is not supported!
Debug/main.moc:-1: In member function 'virtual const QMetaObject* SomeWidget::metaObject() const':
error: 'QObject' is an ambiguous base of 'SomeWidget'
error: 'QObject' is an ambiguous base of 'SomeWidget'
To copy to clipboard, switch view to plain text mode
Bookmarks