PDA

View Full Version : QStackedWidget - paintevent of widget not getting called



el33t
15th February 2011, 02:47
Hi,

Please consider the below codes:

File : mainwindow.cpp

mainwindow::mainwindow()
{
men = new menu1(this);

q1 = new QPushButton("q1", men);
connect(quran1, SIGNAL(clicked()), this, SLOT(qopen()));

qs = new QStackedWidget(this);
qs->addWidget(men);
qs->setCurrentIndex(0);

show();
}

File : men.cpp

menu1::menu1(QWidget *parent) :
QWidget(parent)
{
resize(880, 580);
}

void menu1::paintEvent(QPaintEvent *) // THIS JUST DRAWS A GREEN CURVE
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QPainterPath path;
path.moveTo(qrand() % 80, qrand() % 320);
path.cubicTo(200, 200, 320, 80, 480, 320);
painter.setPen(QPen(Qt::green, 8));
painter.drawPath(path);
}



The problem is, whenever I run the above code, in the output, all I get is the pushbutton but not the green curve which is in the paintevent of menu1.


HOWEVER,

When I remove the QStackedWidget from mainwindow.cpp as shown below, I get both the pushbutton AND the green curve.


mainwindow::mainwindow()
{
men = new menu1(this);

q1 = new QPushButton("q1", men);
connect(quran1, SIGNAL(clicked()), this, SLOT(qopen()));

show();
}


What is the problem with QStackedWidget? How can I get both the pushbutton and the curve to appear with the first example of mainwindow?

Regards.

high_flyer
16th February 2011, 10:33
Try giving the stacked widget a large enough size explicitly.

el33t
16th February 2011, 13:59
Try giving the stacked widget a large enough size explicitly.

Yeah, your trick worked perfectly. Thanks.