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