Results 1 to 19 of 19

Thread: Subclassing a QWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2013
    Posts
    43
    Thanks
    27
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Subclassing a QWidget

    Hi
    I'm trying to understand how to subclass a QWidget.
    I want to create a widget with 2 buttons allong with some drawing elements.
    How does it work in terms of layout for the subclassed widget.
    I ask because aparently the first layout that i use becomes the main layout!
    I attach small small example: layoutwidget.zip

  2. #2
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing a QWidget

    Qt Code:
    1. #ifndef DESENHO_H
    2. #define DESENHO_H
    3.  
    4. #include <QWidget>
    5. #include <QVBoxLayout>
    6. #include <QSpacerItem>
    7. #include <QPushButton>
    8.  
    9.  
    10. class Widget : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. Widget(QWidget *parent = 0);
    16. ~Widget();
    17. void draw(QPainter *painter);
    18.  
    19. protected:
    20. void paintEvent(QPaintEvent *event);
    21.  
    22. private:
    23. QPushButton *btn1, *btn2;
    24. QSpacerItem *spacer;
    25. QVBoxLayout *verticalLayout;
    26. QHBoxLayout *horizontalLayout;
    27. };
    28.  
    29. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "widget.h"
    4.  
    5. Widget::Widget(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. btn1 = new QPushButton(this);
    9. btn2 = new QPushButton(this);
    10. spacer = new QSpacerItem(0,100);
    11. verticalLayout = new QVBoxLayout(this);
    12. horizontalLayout=new QHBoxLayout;
    13. verticalLayout->addStretch(1);
    14. // horizontalLayout->addItem(spacer);
    15. horizontalLayout->addWidget(btn1);
    16. horizontalLayout->addWidget(btn2);
    17. verticalLayout->addLayout(horizontalLayout);
    18. this->setLayout(verticalLayout);
    19. //verticalLayout->addItem(spacer);
    20. //verticalLayout->addWidget(btn1);
    21. //verticalLayout->addWidget(btn2);
    22. }
    23.  
    24. Widget::~Widget()
    25. {
    26.  
    27. }
    28.  
    29. void Widget::paintEvent(QPaintEvent * /* event */)
    30. {
    31. QPainter painter(this);
    32. painter.setRenderHint(QPainter::Antialiasing, true);
    33.  
    34.  
    35. draw(&painter);
    36. }
    37.  
    38. void Widget::draw(QPainter *painter)
    39. {
    40.  
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 

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

    aguleo (18th February 2013)

  4. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Subclassing a QWidget

    You need to re-implement paintEvent of "some drawing elements" and let the "main layout" take care the laying out pushbuttons and "drawing elements".
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    aguleo (18th February 2013)

  6. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Subclassing a QWidget

    You set create a "main" layout and within that you can add another layout if you want to, tell us more about what are you trying to achieve and also take a look at documentation

    Also, you can use forward declaration in headers for all classes that have only pointers or references or are passed as parameter to member function (this makes the compilation faster for bigger projects):
    Qt Code:
    1. #ifndef DESENHO_H
    2. #define DESENHO_H
    3.  
    4. #include <QWidget> //you need this header included because you derive from QWidget
    5. /* you don't need these headers included in your header
    6. -- include those in the .cpp file
    7. #include <QVBoxLayout>
    8. #include <QSpacerItem>
    9. #include <QPushButton>
    10. */
    11. //here you only use forward declaration:
    12. //and so on
    13. class Widget : public QWidget
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. Widget(QWidget *parent = 0);
    19. ~Widget();
    20. void draw(QPainter *painter);
    21.  
    22. protected:
    23. void paintEvent(QPaintEvent *event);
    24.  
    25. private:
    26. QPushButton *btn1, *btn2;
    27. QSpacerItem *spacer;
    28. QVBoxLayout *verticalLayout;
    29. };
    30.  
    31. #endif
    To copy to clipboard, switch view to plain text mode 

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

    aguleo (18th February 2013)

Similar Threads

  1. Replies: 0
    Last Post: 28th October 2011, 18:46
  2. Replies: 1
    Last Post: 16th September 2010, 15:57
  3. Subclassing QWidget and QPushButton
    By ber0y in forum Newbie
    Replies: 7
    Last Post: 24th July 2009, 10:25
  4. subclassing QWidget
    By BeS in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2009, 20:38
  5. Replies: 1
    Last Post: 2nd May 2006, 21:11

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
  •  
Qt is a trademark of The Qt Company.