Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 926
sithereal Avatar asked Nov 23 '11 17:11

sithereal


1 Answers

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.

like image 50
Lwin Htoo Ko Avatar answered Oct 15 '22 00:10

Lwin Htoo Ko