Results 1 to 19 of 19

Thread: Subclassing a QWidget

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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 

  2. The following user says thank you to alrawab 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.