Hello.

Creating a QWidget and change its background colour works fine. But if I try the same with a custom widget which inherits for QWidget it won't work.

This is an minamal example which shows this behaviour:

Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5.  
  6. namespace Ui {
  7. class MainWindow;
  8. }
  9.  
  10. class MyWidget : public QWidget
  11. {
  12. Q_OBJECT
  13.  
  14. public:
  15. explicit MyWidget(QWidget *parent = 0);
  16. };
  17.  
  18. class MainWindow : public QMainWindow
  19. {
  20. Q_OBJECT
  21.  
  22. public:
  23. explicit MainWindow(QWidget *parent = 0);
  24. ~MainWindow();
  25.  
  26. private slots:
  27. void on_pushButton_clicked();
  28.  
  29. private:
  30. Ui::MainWindow *ui;
  31. QWidget *m_pMyWidget;
  32. };
  33.  
  34. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MyWidget::MyWidget(QWidget *parent) :
  5. QWidget(parent)
  6. {
  7.  
  8. }
  9.  
  10. MainWindow::MainWindow(QWidget *parent) :
  11. QMainWindow(parent),
  12. ui(new Ui::MainWindow)
  13. {
  14. ui->setupUi(this);
  15. }
  16.  
  17. MainWindow::~MainWindow()
  18. {
  19. delete ui;
  20. }
  21.  
  22. void MainWindow::on_pushButton_clicked()
  23. {
  24. //m_pMyWidget = new QWidget(this); // <= Works !!!
  25. m_pMyWidget = new MyWidget(this); // <= Does not work !!!
  26. m_pMyWidget->setGeometry(0,0,300,100);
  27. m_pMyWidget->setStyleSheet("background-color:black;");
  28. m_pMyWidget->show();
  29. }
To copy to clipboard, switch view to plain text mode 

What am I doing wrong?