6

I can create and see a QWidget in one of the functions of main window class:

..
// ok
QWidget *w = new QWidget(this);
w->setGeometry(400,300,400,300);
w->setStyleSheet("background-color:white;");
w->show();
..

but when I try to do something similar by creating another class which is derived from QWidget, I can't see anything:

class MyWidget : public QWidget
{
        public:
        MyWidget(QWidget *sParent):QWidget(sParent)
        {
        }
};

        ..
        // nothing visible happens.
        MyWidget *w = new MyWidget(this);
        w->setGeometry(400,300,400,300);
        w->setStyleSheet("background-color:white;");
        w->show();
        ..

what may cause this?

Note: Everything about this question: http://pastebin.com/haCHfqnu

sithereal
  • 1,656
  • 9
  • 16
  • that's like the only code Dave. it's easy to reproduce this situation. but I'm adding the full source to question. – sithereal Nov 24 '11 at 10:21

1 Answers1

4

You can rewrite the paintEvent.

void MyWidget::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

I think that will fix the problem.

Lwin Htoo Ko
  • 2,326
  • 4
  • 26
  • 38
  • But I don't understand: when I derive MyWidget from QWidget, I don't override any of QWidget's functions. basically this means base class version of the functions will be called. so it should act exactly like an ordinary QWidget – sithereal Nov 24 '11 at 10:26
  • I think it is a bug. Only QWidget has this kind of weird behavior. If you create the another kings of widget like QTextEdit or QPushButton, they will behave like the original class. – Lwin Htoo Ko Nov 25 '11 at 06:59
  • 1
    some explanation here: http://falsinsoft.blogspot.com/2015/02/qt-snippet-use-stylesheet-in-qwidget.html "If you derive your class directly from QWidget object and want to apply some stylesheet tags for customize interface you'll find that your stylesheet settings will not work. Just only a simply stylesheet tag for set background color as follow will not have any effect:" – user3528438 Mar 25 '15 at 13:11