PDA

View Full Version : Reuse a Qwidget with a QStackWidget



ucomesdag
24th April 2007, 14:06
Hi,

I'm looking for away to reuse a QWidget and add it multiple times to a QStackWidget.

I need something close to this



QGridLayout *myLayout = new QGridLayout;

QWidget *tmpWidget = new QWidget;
tmpWidget->setLayout(myLayout);

QStackWidget *myStackWidget = new QStackWidget;

for(int i=0;i<10;++i)
{
myStackWidget->insertWidget(i, tmpWidget);
};

Any help is appreciated, thanks.

marcel
24th April 2007, 14:10
This is not possible... A widget cannot have more than one parent ( the stacked widgets page reparents the widget bas soon as you add it ).


Do you need exactly the same instance of the widget in all pages? Then when you change the stacked widget page, just remove the tmpWidget from the previous position and add it to the current position. Not very nice, but...

Ultimately, create 10 widgets, and that's it. If you want the changes from one widget to reflect in the others, just synchronize them with signals/slots.

Regards

high_flyer
24th April 2007, 14:21
if you need to show the same data on various views, you might want to design it with the model/view approach.
http://doc.trolltech.com/4.2/model-view-programming.html

KShots
24th April 2007, 15:25
That or you could make that widget contain static variables (so they all have the same data) and create a new instance for each page of your stacked widget. That should also free you up to make some parts of your widget different on each page.

ucomesdag
25th April 2007, 03:47
Is there else a way to duplicate the widget?

Found the answer myself: No...:o