Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QStackedWidget Class Reference

The QStackedWidget class provides a stack of widgets where only the top widget is visible. More...

    #include <QStackedWidget>

Inherits QFrame.

- List of all members, including inherited members

### Writable Properties

- currentIndex : int

- 5 writable properties inherited from QFrame
- 33 writable properties inherited from QWidget
- 1 writable property inherited from QObject

### Read-Only Properties

- count : int

- 1 read-only property inherited from QFrame
- 20 read-only properties inherited from QWidget

### Public Functions

- QStackedWidget ( QWidget * parent = 0 )
- ~QStackedWidget ()
- int addWidget ( QWidget * w )
- QWidget * currentWidget () const
- int indexOf ( QWidget * w ) const
- int insertWidget ( int index, QWidget * w )
- void removeWidget ( QWidget * w )
- QWidget * widget ( int index ) const

- 2 public functions inherited from QFrame
- 92 public functions inherited from QWidget
- 26 public functions inherited from QObject
- 10 public functions inherited from QPaintDevice

### Public Slots

- void setCurrentIndex ( int )

- 16 public slots inherited from QWidget
- 1 public slot inherited from QObject

### Signals

- void currentChanged ( int index )
- void widgetRemoved ( int index )

- 1 signal inherited from QObject

### Static Public Members

- 4 static public members inherited from QWidget
- 4 static public members inherited from QObject

### Protected Functions

- 42 protected functions inherited from QWidget
- 7 protected functions inherited from QObject

## Detailed Description

The QStackedWidget class provides a stack of widgets where only the top widget is visible.

A stacked box provides a container for a number of child widgets, keeping all of these hidden except the widget at the top of the stack. It can be used to create a user interface similar to the one provided by QTabWidget.

A stacked box can be constructed and populated with a number of child widgets, each of which is typically created without a parent widget:

        QStackedWidget *pages = new QStackedWidget(this);
        pages->addWidget(firstPageWidget);
        pages->addWidget(secondPageWidget);
        pages->addWidget(thirdPageWidget);

The top widget in the stack is the currentWidget(). It can be changed by setting the currentIndex property, using setCurrentIndex(). The index of a given widget inside the stacked box is retrieved with indexOf(). The widget() function returns the widget at a given index position.

It is often useful to be able to control the current widget from other widgets. For example, a combobox could be used to hold the titles of the widgets in a stacked box, and used to change the current widget when an item is selected. This would be achieved by connecting its activated() signal to the setCurrent() slot of the stacked box:

        QComboBox *pageComboBox = new QComboBox(this);
        pageComboBox->insertItem("Page 1");
        pageComboBox->insertItem("Page 2");
        pageComboBox->insertItem("Page 3");
        connect(pageComboBox, SIGNAL(activated(int)),
                pages, SLOT(setCurrentIndex(int)));

If you just need a stacked layout (not a widget), use QStackedLayout instead.

See also QTabWidget.

## Property Documentation

### count : int

This property holds the number of widgets in this layout.

Access functions:

- int count () const

### currentIndex : int

This property holds the index position of the current widget.

The current index is -1 if there is no current widget. The widget at index position 0 is the one that is on top (i.e. the one that is visible).

Access functions:

- int currentIndex () const
- void setCurrentIndex ( int )

See also currentWidget() and indexOf().

## Member Function Documentation

### QStackedWidget::QStackedWidget ( QWidget * parent = 0 )

Constructs a new QStackedWidget as a child of parent.

### QStackedWidget::~QStackedWidget ()

Destroys the object and frees any allocated resources.

### int QStackedWidget::addWidget ( QWidget * w )

Adds w to this box. The first widget added becomes the initial current widget. Returns the index of w in this box.

### void QStackedWidget::currentChanged ( int index ) `[signal]`

This signal is emitted when the current widget is changed. The parameter holds the index of the new current widget, or -1 if there isn't a new one (for example, if there are no widgets in the stacked box).

### QWidget * QStackedWidget::currentWidget () const

Returns the current widget, or 0 if there are no child widgets.

### int QStackedWidget::indexOf ( QWidget * w ) const

Returns the index of w, or -1 if w is not a child.

### int QStackedWidget::insertWidget ( int index, QWidget * w )

Inserts w to this box at position index. If index is out of range, the widget gets appened. The first widget added becomes the initial current widget. Returns the index of w in this box.

### void QStackedWidget::removeWidget ( QWidget * w )

Removes widget w from this layout, but does not delete it.

### QWidget * QStackedWidget::widget ( int index ) const

Returns the widget at position index, or 0 if there is no such widget.

### void QStackedWidget::widgetRemoved ( int index ) `[signal]`

This signal is emitted when the widget at position index is removed.