sithereal
sithereal

Reputation: 1686

QWidget derived class is not visible

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

Upvotes: 6

Views: 2199

Answers (1)

Lwin Htoo Ko
Lwin Htoo Ko

Reputation: 2406

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.

Upvotes: 4

Related Questions