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.

Qt Code:
  1. Widget::Widget(QWidget *parent)
  2. : QWidget(parent)
  3. {
  4. QPushButton *pushButton1 = new QPushButton("Page 1");
  5. QPushButton *pushButton2 = new QPushButton("Page 2");
  6. QVBoxLayout *mainLayout = new QVBoxLayout;
  7. QStackedWidget *mStacked = new QStackedWidget;
  8.  
  9. mStacked->addWidget( commonWidget() ); //Declaration of Page 1
  10. mStacked->addWidget( commonWidget() ); //Declaration of Page 2
  11.  
  12. mainLayout->addWidget(mStacked);
  13. mainLayout->addWidget(pushButton1);
  14. mainLayout->addWidget(pushButton2);
  15. setLayout(mainLayout);
  16.  
  17. }
  18.  
  19. QWidget* Widget::commonWidget()
  20. {
  21. QLabel * mLabel = new QLabel("VariedText");
  22. QHBoxLayout *layout = new QHBoxLayout;
  23. QWidget* mWidget = new QWidget();
  24. layout->addWidget(mLabel);
  25. mWidget->setLayout(layout);
  26. return mWidget;
  27. }
To copy to clipboard, switch view to plain text mode 


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