Results 1 to 6 of 6

Thread: Why can't I inherit both QLineEdit and QPushButton to same class?

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Why can't I inherit both QLineEdit and QPushButton to same class?

    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 ?

    Qt Code:
    1. // A - Base class
    2. // P, Q - Derived classes from A
    3. // Z - Derived from P, Q
    4. // Works fine with this order.
    5.  
    6. #include <iostream>
    7.  
    8. using namespace std;
    9.  
    10. class A
    11. {
    12. public:
    13. A() { cout << "A::Ctor\n"; }
    14. ~A() { cout << "A::Dtor\n"; }
    15. };
    16.  
    17. class P : public A
    18. {
    19. public:
    20. P() { cout << "P::Ctor\n"; }
    21. ~P() { cout << "P::Dtor\n"; }
    22. };
    23.  
    24. class Q : public A
    25. {
    26. public:
    27. Q() { cout << "Q::Ctor\n"; }
    28. ~Q() { cout << "Q::Dtor\n"; }
    29. };
    30.  
    31. class Z : public P, public Q
    32. {
    33. public:
    34. Z() { cout << "Z::Ctor\n"; }
    35. ~Z() { cout << "Z::Dtor\n"; }
    36. };
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. Z obj;
    41. return 0;
    42. }
    To copy to clipboard, switch view to plain text mode 

    But the same kinda inheritance isn't working for Qt classes
    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QLineEdit>
    4. #include <QDebug>
    5.  
    6. class SomeWidget : public QPushButton, public QLineEdit // Both derived from QWidget, which is derived from QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. SomeWidget() { qDebug() << "SomeWidget::Ctor"; }
    11. ~SomeWidget() { qDebug() << "SomeWidget::Dtor"; }
    12. };
    13.  
    14. int main(int argc, char *argv[])
    15. {
    16. QApplication a(argc, argv);
    17.  
    18. SomeWidget wid;
    19. wid.show();
    20.  
    21. return a.exec();
    22. }
    23.  
    24. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Issues I got when I compiled this
    Qt Code:
    1. Warning: Class SomeWidget inherits from two QObject subclasses QPushButton and QLineEdit. This is not supported!
    2. Debug/main.moc:-1: In member function 'virtual const QMetaObject* SomeWidget::metaObject() const':
    3. error: 'QObject' is an ambiguous base of 'SomeWidget'
    4. error: 'QObject' is an ambiguous base of 'SomeWidget'
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why can't I inherit both QLineEdit and QPushButton to same class?

    This kind of diamond shaped derivation is almost never a good idea, even if it is supported by C++ in general.

    It gets you into all kinds of ambiguous situations, e.g. if your class A has a virtual method and both P and Q implement it, which one of the two implementations is called when you call the method on Z?

    In the case of QObject subclasses this kind of hierachy is made unavailable to make the QObject addons work properly, e.g. the code generated by MOC when it encounters classes with the Q_OBJECT marker, etc.

    What is you actual use case?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why can't I inherit both QLineEdit and QPushButton to same class?

    There's nothing aimed at. I was just trying to see what happens if I derive from both QPushButton & QLineEdit.
    Thank you for giving me wisdom.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why can't I inherit both QLineEdit and QPushButton to same class?

    Quote Originally Posted by rawfool View Post
    There's nothing aimed at. I was just trying to see what happens if I derive from both QPushButton & QLineEdit.
    Thank you for giving me wisdom.
    Usually to get rid of the diamond you can substitute inheritance with composition.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Why can't I inherit both QLineEdit and QPushButton to same class?

    Composition, do you mean like this - ?
    Qt Code:
    1. class SomeWidget : public QLineEdit
    2. {
    3. Q_OBJECT
    4. QPushButtton * clearButton;
    5. public:
    6. SomeWidget();
    7. ~SomeWidget();
    8. ....
    9. private slots:
    10. void onClearButton_Click(void);
    11. ...
    12. };
    To copy to clipboard, switch view to plain text mode 

    Do you mean - Achieving all properties of widget by appropriately sub-classing required class(es) & using some class(es) as members ?
    Last edited by rawfool; 12th May 2014 at 11:42.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why can't I inherit both QLineEdit and QPushButton to same class?

    More likely:

    Qt Code:
    1. class Widget : public QWidget {
    2. public:
    3. Widget(QWidget *parent = 0) : QWidget(parent) {
    4. m_lineEdit = ...;
    5. m_pushButton = ...;
    6. QLayout *l = ....
    7. l->addWidget(m_lineEdit);
    8. l->addWidget(m_pushButton);
    9. }
    10.  
    11. QLineEdit *lineEdit() const { return m_lineEdit; }
    12. QPushButton* pushButton() const { return m_pushButton; }
    13. private:
    14. QLineEdit *m_lineEdit = 0;
    15. QPushButton *m_pushButton = 0;
    16. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 12th May 2014 at 12:16.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    rawfool (12th May 2014)

Similar Threads

  1. Replies: 1
    Last Post: 22nd November 2013, 18:18
  2. QLineEdit and QPushButton heights
    By ecir.hana in forum Qt Programming
    Replies: 11
    Last Post: 11th April 2013, 04:24
  3. Replies: 2
    Last Post: 2nd May 2011, 09:10
  4. Replies: 4
    Last Post: 15th September 2010, 01:22
  5. Replies: 3
    Last Post: 28th January 2007, 18:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.