Hi,
Please consider the below codes:
File : mainwindow.cpp
mainwindow::mainwindow()
{
men = new menu1(this);
connect(quran1, SIGNAL(clicked()), this, SLOT(qopen()));
qs->addWidget(men);
qs->setCurrentIndex(0);
show();
}
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();
}
To copy to clipboard, switch view to plain text mode
File : men.cpp
{
resize(880, 580);
}
void menu1
::paintEvent(QPaintEvent *) // THIS JUST DRAWS A GREEN CURVE {
painter.
setRenderHint(QPainter::Antialiasing,
true);
path.moveTo(qrand() % 80, qrand() % 320);
path.cubicTo(200, 200, 320, 80, 480, 320);
painter.
setPen(QPen(Qt
::green,
8));
painter.drawPath(path);
}
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);
}
To copy to clipboard, switch view to plain text mode
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);
connect(quran1, SIGNAL(clicked()), this, SLOT(qopen()));
show();
}
mainwindow::mainwindow()
{
men = new menu1(this);
q1 = new QPushButton("q1", men);
connect(quran1, SIGNAL(clicked()), this, SLOT(qopen()));
show();
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks