PDA

View Full Version : QScrollArea containing multiple widgets



Mercurial
4th February 2011, 00:19
I want a QScrollArea that contains multiple widgets but on top of each other (like a deck of cards):


scrollArea->setWidget(widget1);
widget1 appears

scrollArea->setWidget(widget2);
widget2 appears

scrollArea->setWidget(widget1);
SEGMENTATION FAULT

QScrollArea::setWidget(QWidget*) deletes the old widget before setting a new one, thus causing a segmentation fault. I've thought about keeping one widget and switching layouts, but Qt reference says:
"If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout." meaning old layout would be deleted before setting a new one. I have my widgets stored in a QMap and I'd like to switch em during runtime. Switching layouts would work for me aswell, np. The only solution coming to my mind is copying widgets(layouts)before setWidget() call, but not sure how smart that is.

Thanks in advance

tbscope
4th February 2011, 04:42
There is of course MDI and QGraphicsView too. Both let you easily add widgets on top of each other and have scrolling capabilities.

As for using a scrollArea. Why not add a single parent widget and use that parent widget to display the other widgets?
You can use layouts anyway if you want to put them on top of each other.

ChrisW67
4th February 2011, 04:55
I want a QScrollArea that contains multiple widgets but on top of each other (like a deck of cards)

If you mean like a deck of cards where only the top card is visible at any time then look at QStackedWidget as the scrolled widget.

If you mean a vertical list of widgets then you should combine a persistent QWidget with QVBoxLayout. You can add and remove widgets in the layout with addWidget(), insertWidget(), and removeWidget().

Mercurial
4th February 2011, 10:08
QStackedWidget solved my problems. Thanks alot.