Widget Stacks
Another useful widget for managing layouts is QWidgetStack. This widget contains a set of child widgets, or "pages", and shows only one at a time, hiding the others from the user. The pages are numbered from 0. If we want to make a specific child widget visible, we can call raiseWidget() with either a page number or a pointer to the child widget.
The QWidgetStack itself is invisible and provides no intrinsic means for the user to change page. The small arrows and the dark gray frame in Figure 6.7 are provided by Qt Designer to make the QWidgetStack easier to design with.
Figure 6.7. QWidgetStack
The Configure dialog shown in Figure 6.8 is an example that uses QWidgetStack. The dialog consists of a QListBox on the left and a QWidgetStack on the right. Each item in the QListBox corresponds to a different page in the QWidgetStack. Forms like this are very easy to create using Qt Designer:
- Create a new form based on the "Dialog" or the "Widget" template.
- Add a list box and a widget stack to the form.
- Fill each widget stack page with child widgets and layouts. (To create a new page, right-click and choose Add Page; to switch pages, click the tiny left or right arrow located at the top-right of the widget stack.)
- Lay the widgets out side by side using a horizontal layout.
- Connect the list box's highlighted(int) signal to the widget stack's raiseWidget(int) slot.
- Set the value of the list box's currentItem property to 0.
Figure 6.8. The Configure dialog
Since we have implemented page-switching using predefined signals and slots, the dialog will exhibit the correct page-switching behavior when previewed in Qt Designer.