Results 1 to 7 of 7

Thread: Problems with laying out custom widget

  1. #1
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems with laying out custom widget

    When I place five objects of MTR::Button class on MyWidget object (simple testing area) using QVBoxLayout I receive five buttons on one pile!

    How to achieve an ordinary vertical box layout effect in this case?

    MTRButton.h:
    Qt Code:
    1. #ifndef MTR_Button_H
    2. #define MTR_Button_H
    3. #include <QAbstractButton>
    4. class QSize;
    5.  
    6. namespace MTR {
    7. class Button : public QAbstractButton {
    8. Q_OBJECT
    9. public:
    10. Button(QWidget *parent = 0);
    11. Button(QString tekst, QWidget *parent = 0);
    12. protected:
    13. void paintEvent(QPaintEvent *event);
    14. QSize sizeHint() const;
    15. QSize minimumSizeHint() const;
    16. void mouseMoveEvent(QMouseEvent *event);
    17. void mousePressEvent(QMouseEvent *event);
    18. void mouseReleaseEvent(QMouseEvent *event);
    19. private:
    20. bool hovered;
    21. QFont font;
    22. };
    23. }
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    its implementation
    MTRButton.cpp
    Qt Code:
    1. #include "MTRButton.h"
    2. #include <QtGlobal>
    3. #include <QtCore>
    4. #include <QtGui>
    5.  
    6. namespace MTR {
    7. Button::Button(QWidget *parent) : QAbstractButton(parent) { setMouseTracking(true); hovered = false; }
    8. Button::Button(QString tekst,QWidget *parent) : QAbstractButton(parent) { setMouseTracking(true); hovered = false; this->setText(tekst); }
    9. void Button::paintEvent(QPaintEvent * /* event */) {
    10. QPainter painter(this);
    11. painter.setRenderHint(QPainter::Antialiasing, true);
    12. QPen borderPen(QColor(Qt::black),1);
    13. painter.setPen(borderPen);
    14. QBrush ribbonBrush(palette().dark());
    15. painter.setBrush(ribbonBrush);
    16. painter.drawRoundRect(5,5,this->width() -10, this->height() - 10,25,25);
    17. QBrush insideBrush(palette().mid());
    18. if(isDown()) insideBrush = palette().light();
    19. painter.setBrush(insideBrush);
    20. painter.drawRoundRect(10,10,this->width() -20, this->height() - 20,25,25);
    21. if((this->height()-20)/2 >= 0) {
    22. QRadialGradient gradRed(QPointF(15+(this->height()-20)/2/4, 15+(this->height()-20)/2/3), (this->height()-20)/2);
    23. if(!hovered) {
    24. gradRed.setColorAt(0,Qt::white);
    25. gradRed.setColorAt(1,Qt::red);
    26. } else {
    27. gradRed.setColorAt(0,Qt::white);
    28. gradRed.setColorAt(1,Qt::black);
    29. }
    30. QBrush circBrush(gradRed);
    31. painter.setBrush(circBrush);
    32. painter.drawEllipse(15,15,(this->height()-20)/2,(this->height()-20)/2);
    33.  
    34. QRadialGradient gradGreen(QPointF(15+(this->height()-20)/2 + 2 +(this->height()-20)/2/4, 15+(this->height()-20)/2/3), (this->height()-20)/2);
    35. if(hovered || isDown()) {
    36. gradGreen.setColorAt(0,Qt::white);
    37. gradGreen.setColorAt(1,Qt::green);
    38. } else {
    39. gradGreen.setColorAt(0,Qt::white);
    40. gradGreen.setColorAt(1,Qt::black);
    41. }
    42. circBrush = QBrush(gradGreen);
    43. painter.setBrush(circBrush);
    44. painter.drawEllipse(15+(this->height()-20)/2 + 2,15,(this->height()-20)/2,(this->height()-20)/2);
    45. }
    46. painter.setPen(borderPen);
    47. font = QFont(tr("Arial"),10,50,false);
    48. painter.setFont(font);
    49. QRect textRect(this->height(), 5 + this->height()/6, this->width() - this->height() - 20, this->height()-20);
    50. painter.drawText(textRect,Qt::AlignTop | Qt::AlignHCenter, this->text());
    51. }
    52. QSize Button::minimumSizeHint() const {
    53. return QSize(200,40);
    54. }
    55. QSize Button::sizeHint() const {
    56. QFontMetrics fm(font);
    57. int tekstSzer = fm.width(this->text());
    58. return QSize(tekstSzer + 28 + 40,40);
    59. }
    60. void Button::mouseMoveEvent(QMouseEvent *event) {
    61. QRect rect(10,10,this->width()-20,this->height()-20);
    62. if(rect.contains(event->pos())) {
    63. if(hovered) return;
    64. else { hovered = true; update(); }
    65. }
    66. else {
    67. if(!hovered) return;
    68. else { hovered = false; update(); }
    69. }
    70. }
    71. void Button::mousePressEvent(QMouseEvent * /* event */) {
    72. setDown(true);
    73. }
    74. void Button::mouseReleaseEvent(QMouseEvent *event) {
    75. setDown(false);
    76. QRect rect(10,10,this->width()-20,this->height()-20);
    77. if(rect.contains(event->pos())) emit clicked();
    78. }
    79. }
    To copy to clipboard, switch view to plain text mode 

    Five such objects are placed in a QVBoxLayout on MyWidget
    MyWidget.h
    Qt Code:
    1. #ifndef TESTWIDGET
    2. #define TESTWIDGET
    3. #include <QWidget>
    4. namespace MTR {
    5. class Button;
    6. }
    7. class MyWidget : public QWidget {
    8. Q_OBJECT
    9. public:
    10. MyWidget(QWidget *parent = 0);
    11. public slots:
    12. void TestujPrzycisk() { qDebug("Click!"); }
    13. private:
    14. MTR::Button *b1;
    15. MTR::Button *b2;
    16. MTR::Button *b3;
    17. MTR::Button *b4;
    18. MTR::Button *b5;
    19. };
    20. #endif
    To copy to clipboard, switch view to plain text mode 

    MyWidget.cpp
    Qt Code:
    1. #include "MyWidget.h"
    2. #include "MTRButton.h"
    3. #include <QtCore>
    4. #include <QtGui>
    5.  
    6. MyWidget::MyWidget(QWidget *parent) : QWidget(parent) {
    7. b1 = new MTR::Button(QString::fromUtf8("Rozpocznij grę"),this);
    8. b2 = new MTR::Button(QString::fromUtf8("Wczytaj grę"),this);
    9. b3 = new MTR::Button(QString::fromUtf8("Wyniki"),this);
    10. b4 = new MTR::Button(QString::fromUtf8("Zasady"),this);
    11. b5 = new MTR::Button(QString::fromUtf8("Wyjdź"),this);
    12.  
    13. qvb.addWidget(b1);
    14. qvb.addWidget(b2);
    15. qvb.addWidget(b3);
    16. qvb.addWidget(b4);
    17. qvb.addWidget(b5);
    18. setLayout(&qvb);
    19. }
    To copy to clipboard, switch view to plain text mode 

    and finally main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QtCore>
    3. #include <QtGui>
    4. #include "MTRButton.h"
    5. #include "MyWidget.h"
    6.  
    7. int main(int argc, char * argv[]) {
    8. QApplication app(argc, argv);
    9. MyWidget widget;
    10. QPalette orangePalette;
    11. orangePalette.setColorGroup(QPalette::Active,QBrush(Qt::black),QBrush(QColor(255,180,51)),
    12. QBrush(QColor(255,212,71)),QBrush(QColor(70,100,105)),QBrush(QColor(255,118,54)),
    13. QBrush(QColor(Qt::black)),QBrush(QColor(Qt::gray)),QBrush(QColor(Qt::black)),QBrush(QColor(255,180,51)));
    14.  
    15. orangePalette.setColorGroup(QPalette::Inactive,QBrush(Qt::black),QBrush(QColor(255,180,51)),
    16. QBrush(QColor(255,212,71)),QBrush(QColor(70,100,105)),QBrush(QColor(255,118,54)),
    17. QBrush(QColor(Qt::black)),QBrush(QColor(Qt::gray)),QBrush(QColor(Qt::black)),QBrush(QColor(255,180,51)));
    18. widget.setPalette(orangePalette);
    19. widget.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with laying out custom widget

    Don't allocate the layout on the stack. It get's deleted as soon the constructor exists.
    Do it on the heap( i.e. QVBoxLayout * qvb = new QVBoxLayout(this));

    Regards

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with laying out custom widget

    "QVBoxLayout qvb" gets automatically destructed while going out of scope by the time MyWidge constructor ends.

    Try something like this:
    Qt Code:
    1. QVBoxLayout* qvb = new QVBoxLayout(this);
    2. qvb->addWidget(b1);
    3. ...
    4. setLayout(qvb);
    To copy to clipboard, switch view to plain text mode 

    Edit: D*mn you Marcel!
    J-P Nurmi

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with laying out custom widget

    Quote Originally Posted by jpn View Post
    [/code]Edit: D*mn you Marcel!
    Don't worry. These days I have time to answer only a few posts/day( not even those ), so the rest of them are yours.

    Regards

  5. #5
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with laying out custom widget

    Bloody hell!
    It is obvious!

    A short break from coding and such a stupid mistake.
    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Problems with laying out custom widget

    Quote Originally Posted by marcel View Post
    These days I have time to answer only a few posts/day( not even those ), so the rest of them are yours.
    Hell, they are! I want my share too!

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with laying out custom widget

    Hell, they are! I want my share too!
    I meant working days, not weekend .

    Regards

Similar Threads

  1. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12
  2. Custom tab widget question
    By PrimeCP in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 11:17
  3. Custom Shape Widget (resize)
    By PiXeL16 in forum Qt Programming
    Replies: 7
    Last Post: 12th February 2007, 07:00
  4. Replies: 1
    Last Post: 5th November 2006, 23:50
  5. Replies: 4
    Last Post: 24th March 2006, 22:50

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.