Pembar
13th May 2009, 17:48
Hey guys,
I am trying to have a common widget which is going to appear on several pages of a stackedwidget. In my example below, I did not connect the pushButtons but assume that pushButton1 will ask mStacked to show the first widget while pushButton2 will ask mStacked to show the second widget.
I've been declaring a few widgets as follows:
Note: Not complete code, but you get the idea.
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QPushButton *pushButton1 = new QPushButton("Page 1");
QPushButton *pushButton2 = new QPushButton("Page 2");
QVBoxLayout *mainLayout = new QVBoxLayout;
QStackedWidget *mStacked = new QStackedWidget;
mStacked->addWidget( commonWidget() ); //Declaration of Page 1
mStacked->addWidget( commonWidget() ); //Declaration of Page 2
mainLayout->addWidget(mStacked);
mainLayout->addWidget(pushButton1);
mainLayout->addWidget(pushButton2);
setLayout(mainLayout);
}
QWidget* Widget::commonWidget()
{
QLabel * mLabel = new QLabel("VariedText");
QHBoxLayout *layout = new QHBoxLayout;
QWidget* mWidget = new QWidget();
layout->addWidget(mLabel);
mWidget->setLayout(layout);
return mWidget;
}
I have two main questions:
1. Is this a good way of declaring a commonWidget?
2. If it's the usual way, is there a way to ensure that only one instance of mLabel is created? In otherwords, when I change the value of mLabel on one page, the same value is displayed on the other pages.
Thanks much.
Regards,
Pembar
I am trying to have a common widget which is going to appear on several pages of a stackedwidget. In my example below, I did not connect the pushButtons but assume that pushButton1 will ask mStacked to show the first widget while pushButton2 will ask mStacked to show the second widget.
I've been declaring a few widgets as follows:
Note: Not complete code, but you get the idea.
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QPushButton *pushButton1 = new QPushButton("Page 1");
QPushButton *pushButton2 = new QPushButton("Page 2");
QVBoxLayout *mainLayout = new QVBoxLayout;
QStackedWidget *mStacked = new QStackedWidget;
mStacked->addWidget( commonWidget() ); //Declaration of Page 1
mStacked->addWidget( commonWidget() ); //Declaration of Page 2
mainLayout->addWidget(mStacked);
mainLayout->addWidget(pushButton1);
mainLayout->addWidget(pushButton2);
setLayout(mainLayout);
}
QWidget* Widget::commonWidget()
{
QLabel * mLabel = new QLabel("VariedText");
QHBoxLayout *layout = new QHBoxLayout;
QWidget* mWidget = new QWidget();
layout->addWidget(mLabel);
mWidget->setLayout(layout);
return mWidget;
}
I have two main questions:
1. Is this a good way of declaring a commonWidget?
2. If it's the usual way, is there a way to ensure that only one instance of mLabel is created? In otherwords, when I change the value of mLabel on one page, the same value is displayed on the other pages.
Thanks much.
Regards,
Pembar