Results 1 to 8 of 8

Thread: Subclassing QWidget and QPushButton

  1. #1
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Subclassing QWidget and QPushButton

    Hello,
    i have some problems by subclassing QWidget and QPushButton

    the error is :
    main.cpp: In member function `virtual void PushButton:aintEvent(QPaintEvent*)':
    main.cpp:33: error: `QPaintDevice' is an ambiguous base of `PushButton'
    The entire code is :
    Qt Code:
    1. #include <QtCore/QDebug>
    2.  
    3. #include <QtGui/QApplication>
    4. #include <QtGui/QPainter>
    5. #include <QtGui/QPaintEvent>
    6. #include <QtGui/QPushButton>
    7. #include <QtGui/QWidget>
    8.  
    9. class Widget : public QWidget
    10. {
    11. public:
    12. Widget() : QWidget() {}
    13. virtual void doTreatment() {qDebug() << "Treatment";}
    14. };
    15.  
    16. class PushButton : public Widget, public QPushButton
    17. {
    18. public:
    19. PushButton() : Widget(), QPushButton() {setText("Push Button");}
    20. void paintEvent(QPaintEvent *event);
    21. };
    22.  
    23. void PushButton::paintEvent(QPaintEvent *event)
    24. {
    25. QPushButton::paintEvent(event);
    26.  
    27. QPixmap pixmap("image.png");
    28. QPoint point = (event->rect()).center();
    29. point.setX(point.x() - pixmap.rect().center().x());
    30. point.setY(point.y() - pixmap.rect().center().y());
    31.  
    32. QPainter painter(this);
    33. painter.drawPixmap(point, pixmap);
    34. }
    35.  
    36. int main(int argc, char *argv[])
    37. {
    38. QApplication a(argc, argv);
    39.  
    40. Widget *widget = new PushButton();
    41. widget->doTreatment();
    42. delete widget;
    43.  
    44. return a.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 



    If you have any ideas, thanks...

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subclassing QWidget and QPushButton

    you cannot inherit from two QObject clases...

    EDIT
    your widget class only has one virtual fucntion... so you can just skip the QWidget inheritance and the code will work.... but one more thing... inherit QPushButton first and then Widget otherwise u will get other errors..

    EDIT2

    do not delete the button in main()... your window will not show up..
    Last edited by nish; 24th July 2009 at 09:44.

  3. The following user says thank you to nish for this useful post:

    ber0y (24th July 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Thumbs up Re: Subclassing QWidget and QPushButton

    Thanks you !!!

    My first problem was that i inherit Widget from QWidget to have access to QWidget's methods. I removed it and i just have to cast to have access to this.
    Thanks again.

    Here the new code :

    Qt Code:
    1. #include <QtCore/QDebug>
    2.  
    3. #include <QtGui/QApplication>
    4. #include <QtGui/QPainter>
    5. #include <QtGui/QPaintEvent>
    6. #include <QtGui/QPushButton>
    7. #include <QtGui/QWidget>
    8.  
    9. class Widget
    10. {
    11. public:
    12. Widget() {}
    13. virtual void doTreatment() {qDebug() << "Treatment";}
    14. };
    15.  
    16. class PushButton : public QPushButton, public Widget
    17. {
    18. public:
    19. PushButton() : QPushButton(), Widget() {setText("Push Button");}
    20. void paintEvent(QPaintEvent *event);
    21. };
    22.  
    23. void PushButton::paintEvent(QPaintEvent *event)
    24. {
    25. QPushButton::paintEvent(event);
    26.  
    27. QPixmap pixmap("image.png");
    28. QPoint point = (event->rect()).center();
    29. point.setX(point.x() - pixmap.rect().center().x());
    30. point.setY(point.y() - pixmap.rect().center().y());
    31.  
    32. QPainter painter(this);
    33. painter.drawPixmap(point, pixmap);
    34. }
    35.  
    36. int main(int argc, char *argv[])
    37. {
    38. QApplication a(argc, argv);
    39.  
    40. Widget *widget = new PushButton();
    41. widget->doTreatment();
    42. ((PushButton*)widget)->show();
    43.  
    44. return a.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 


  5. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QWidget and QPushButton

    void PushButton:aintEvent(QPaintEvent *event)
    declare it as protected .. its a virtual function ..
    "Behind every great fortune lies a crime" - Balzac

  6. The following user says thank you to wagmare for this useful post:

    ber0y (24th July 2009)

  7. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subclassing QWidget and QPushButton

    ((PushButton*)widget)->show();
    you dont have to cast it... widget->show() will do

  8. #6
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Subclassing QWidget and QPushButton

    Quote Originally Posted by MrDeath View Post
    you dont have to cast it... widget->show() will do
    No a Widget instance do not know the show method. The compiler refuses it

  9. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subclassing QWidget and QPushButton

    Quote Originally Posted by ber0y View Post
    No a Widget instance do not know the show method. The compiler refuses it
    yeah... my fault... you have named your class as widget... that confused me...

  10. #8
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Subclassing QWidget and QPushButton

    yeah because when i created it i subclassed QWidget! ;-)
    thanks wagmare too !

Similar Threads

  1. Replies: 0
    Last Post: 11th November 2008, 15:36
  2. Qpushbutton
    By iamhere in forum Qt Programming
    Replies: 5
    Last Post: 15th October 2008, 04:40
  3. Dynamic updates of a QWidget in a QScrollArea
    By plamkata in forum Qt Programming
    Replies: 2
    Last Post: 20th July 2008, 23:45
  4. Dynamic widget not displayed upon Qwidget
    By ashukla in forum Qt Programming
    Replies: 5
    Last Post: 17th December 2007, 11:11
  5. Replies: 3
    Last Post: 8th March 2007, 14:54

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.