I have problems with this code:
(MainWindow.cpp)
Qt Code:
  1. MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent)
  2. {
  3. QWidget * widget = new QWidget;
  4. QVBoxLayout * layout = new QVBoxLayout;
  5. MyWidget * w = new MyWidget;
  6. w->setRectBackgroundColor(255,255,255,255);
  7. w->setHeight(110);
  8. w->show();
  9. layout->addWidget(w,0);
  10. widget->setLayout(layout);
  11. setCentralWidget(widget);
  12. }
To copy to clipboard, switch view to plain text mode 
(MyWidget.h)
Qt Code:
  1. class MyWidget : public QWidget
  2. {
  3. Q_OBJECT
  4. public:
  5. explicit MyWidget(QWidget *parent = 0);
  6. ~MyWidget(void){ };
  7. void setHeight(int height);
  8. void setRectBackgroundColor(int r,int g, int b, int a);
  9. void setRectBackgroundColor(const QColor &bgColor);
  10. protected:
  11. virtual void paintEvent(QPaintEvent * e);
  12. private:
  13. QColor rectBgColor;
  14. int rectHeight;
  15. };
To copy to clipboard, switch view to plain text mode 
(MyWidget.cpp)
Qt Code:
  1. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
  2. {
  3. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  4. }
  5. void MyWidget::setRectBackgroundColor(const QColor &bgColor)
  6. {
  7. rectBgColor = bgColor;
  8. }
  9. void MyWidget::setRectBackgroundColor(int r, int g, int b, int a)
  10. {
  11. rectBgColor = QColor(r,g,b,a);
  12. }
  13. void MyWidget::setHeight(int height)
  14. {
  15. rectHeight = height;
  16. }
  17. void MyWidget::paintEvent(QPaintEvent *e)
  18. {
  19. QPainter painter;
  20. painter.begin(this);
  21. painter.setPen(Qt::NoPen);
  22. painter.setBrush(QBrush(rectBgColor,Qt::SolidPattern));
  23. painter.drawRect(QRect(0,0,rect().width()+1,rectHeight));
  24. painter.end();
  25. }
To copy to clipboard, switch view to plain text mode 

Problem is: In main window it doesn't show any white rectangle... I think is problem with body of MainWindow constructor, because on QWidget window it is working. I am so unhappy with this problem.