QStackedLayout Class Reference

#include <qstackedlayout.h>

Inheritance diagram for QStackedLayout:

Inheritance graph
[legend]
Collaboration diagram for QStackedLayout:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QStackedLayout class provides a stack of widgets where only one widget is visible at a time.

QStackedLayout can be used to create a user interface similar to the one provided by QTabWidget. There is also a convenience QStackedWidget class built on top of QStackedLayout.

A QStackedLayout can be populated with a number of child widgets ("pages"). For example:

snippets/qstackedlayout/main.cpp firstPageWidget QComboBox

QVBoxLayout QVBoxLayout mainLayout->addLayout setLayout

QStackedLayout provides no intrinsic means for the user to switch page. This is typically done through a QComboBox or a QListWidget that stores the titles of the QStackedLayout's pages. For example:

snippets/qstackedlayout/main.cpp QComboBox SLOT

When populating a layout, the widgets are added to an internal list. The indexOf() function returns the index of a widget in that list. The widgets can either be added to the end of the list using the addWidget() function, or inserted at a given index using the insertWidget() function. The removeWidget() function removes the widget at the given index from the layout. The number of widgets contained in the layout, can be obtained using the count() function.

The widget() function returns the widget at a given index position. The index of the widget that is shown on screen is given by currentIndex() and can be changed using setCurrentIndex(). In a similar manner, the currently shown widget can be retrieved using the currentWidget() function, and altered using the setCurrentWidget() function.

Whenever the current widget in the layout changes or a widget is removed from the layout, the currentChanged() and widgetRemoved() signals are emitted respectively.

See also:
QStackedWidget, QTabWidget

Definition at line 35 of file qstackedlayout.h.

Public Slots

void setCurrentIndex (int index)
void setCurrentWidget (QWidget *w)

Signals

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

Public Member Functions

 QStackedLayout ()
 QStackedLayout (QWidget *parent)
 QStackedLayout (QLayout *parentLayout)
 ~QStackedLayout ()
int addWidget (QWidget *w)
int insertWidget (int index, QWidget *w)
QWidgetcurrentWidget () const
int currentIndex () const
QWidgetwidget (int) const
int count () const
void addItem (QLayoutItem *item)
QSize sizeHint () const
QSize minimumSize () const
QLayoutItemitemAt (int) const
QLayoutItemtakeAt (int)
void setGeometry (const QRect &rect)


Constructor & Destructor Documentation

QStackedLayout::QStackedLayout (  ) 

Constructs a QStackedLayout with no parent.

This QStackedLayout must be installed on a widget later on to become effective.

See also:
addWidget(), insertWidget()

Definition at line 125 of file qstackedlayout.cpp.

00126     : QLayout(*new QStackedLayoutPrivate, 0, 0)
00127 {
00128 }

QStackedLayout::QStackedLayout ( QWidget parent  )  [explicit]

Constructs a new QStackedLayout with the given parent.

This layout will install itself on the parent widget and manage the geometry of its children.

Definition at line 136 of file qstackedlayout.cpp.

00137     : QLayout(*new QStackedLayoutPrivate, 0, parent)
00138 {
00139 }

QStackedLayout::QStackedLayout ( QLayout parentLayout  )  [explicit]

Constructs a new QStackedLayout and inserts it into the given parentLayout.

Definition at line 145 of file qstackedlayout.cpp.

00146     : QLayout(*new QStackedLayoutPrivate, parentLayout, 0)
00147 {
00148 }

QStackedLayout::~QStackedLayout (  ) 

Destroys this QStackedLayout. Note that the layout's widgets are not destroyed.

Definition at line 154 of file qstackedlayout.cpp.

References d, and qDeleteAll().

00155 {
00156     Q_D(QStackedLayout);
00157     qDeleteAll(d->list);
00158 }

Here is the call graph for this function:


Member Function Documentation

int QStackedLayout::addWidget ( QWidget widget  ) 

Adds the given widget to the end of this layout and returns the index position of the widget.

If the QStackedLayout is empty before this function is called, the given widget becomes the current widget.

See also:
insertWidget(), removeWidget(), setCurrentWidget()

Reimplemented from QLayout.

Definition at line 169 of file qstackedlayout.cpp.

References d, insertWidget(), and QLayoutItem::widget().

Referenced by addItem().

00170 {
00171     Q_D(QStackedLayout);
00172     return insertWidget(d->list.count(), widget);
00173 }

Here is the call graph for this function:

int QStackedLayout::insertWidget ( int  index,
QWidget widget 
)

Inserts the given widget at the given index in this QStackedLayout. If index is out of range, the widget is appended (in which case it is the actual index of the widget that is returned).

If the QStackedLayout is empty before this function is called, the given widget becomes the current widget.

Inserting a new widget at an index less than or equal to the current index will increment the current index, but keep the current widget.

See also:
addWidget(), removeWidget(), setCurrentWidget()

Definition at line 189 of file qstackedlayout.cpp.

References QLayout::addChildWidget(), d, QWidget::hide(), QLayout::invalidate(), QWidget::lower(), qMin(), setCurrentIndex(), and QLayoutItem::widget().

Referenced by addWidget().

00190 {
00191     Q_D(QStackedLayout);
00192     addChildWidget(widget);
00193     index = qMin(index, d->list.count());
00194     if (index < 0)
00195         index = d->list.count();
00196     QWidgetItem *wi = new QWidgetItem(widget);
00197     d->list.insert(index, wi);
00198     invalidate();
00199     if (d->index < 0) {
00200         setCurrentIndex(index);
00201     } else {
00202         if (index <= d->index)
00203             ++d->index;
00204         widget->hide();
00205         widget->lower();
00206     }
00207     return index;
00208 }

Here is the call graph for this function:

QWidget * QStackedLayout::currentWidget (  )  const

Returns the current widget, or 0 if there are no widgets in this layout.

See also:
currentIndex(), setCurrentWidget()

Definition at line 349 of file qstackedlayout.cpp.

References d.

Referenced by setCurrentIndex(), and setGeometry().

00350 {
00351     Q_D(const QStackedLayout);
00352     return d->index >= 0 ? d->list.at(d->index)->widget() : 0;
00353 }

int QStackedLayout::currentIndex (  )  const

Definition at line 317 of file qstackedlayout.cpp.

References d.

00318 {
00319     Q_D(const QStackedLayout);
00320     return d->index;
00321 }

QWidget * QStackedLayout::widget ( int  index  )  const

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

See also:
currentWidget(), indexOf()

Definition at line 361 of file qstackedlayout.cpp.

References d.

00362 {
00363     Q_D(const QStackedLayout);
00364      if (index < 0 || index >= d->list.size())
00365         return 0;
00366     return d->list.at(index)->widget();
00367 }

int QStackedLayout::count (  )  const [virtual]

Must be implemented in subclasses to return the number of items in the layout.

See also:
itemAt()

Implements QLayout.

Definition at line 375 of file qstackedlayout.cpp.

References d.

00376 {
00377     Q_D(const QStackedLayout);
00378     return d->list.size();
00379 }

void QStackedLayout::addItem ( QLayoutItem item  )  [virtual]

Implements QLayout.

Definition at line 385 of file qstackedlayout.cpp.

References addWidget(), qWarning(), and QLayoutItem::widget().

00386 {
00387     QWidget *widget = item->widget();
00388     if (widget) {
00389         addWidget(widget);
00390         delete item;
00391     } else {
00392         qWarning("QStackedLayout::addItem: Only widgets can be added");
00393     }
00394 }

Here is the call graph for this function:

QSize QStackedLayout::sizeHint (  )  const [virtual]

Implements QLayoutItem.

Definition at line 399 of file qstackedlayout.cpp.

References d, i, n, s, QWidget::sizeHint(), and QLayoutItem::widget().

00400 {
00401     Q_D(const QStackedLayout);
00402     QSize s(0, 0);
00403     int n = d->list.count();
00404 
00405     for (int i = 0; i < n; ++i)
00406         if (QWidget *widget = d->list.at(i)->widget())
00407             s = s.expandedTo(widget->sizeHint());
00408     return s;
00409 }

Here is the call graph for this function:

QSize QStackedLayout::minimumSize (  )  const [virtual]

Reimplemented from QLayout.

Definition at line 414 of file qstackedlayout.cpp.

References d, i, n, qSmartMinSize(), s, and QLayoutItem::widget().

00415 {
00416     Q_D(const QStackedLayout);
00417     QSize s(0, 0);
00418     int n = d->list.count();
00419 
00420     for (int i = 0; i < n; ++i)
00421         if (QWidget *widget = d->list.at(i)->widget())
00422             s = s.expandedTo(qSmartMinSize(widget));
00423     return s;
00424 }

Here is the call graph for this function:

QLayoutItem * QStackedLayout::itemAt ( int  index  )  const [virtual]

Implements QLayout.

Definition at line 213 of file qstackedlayout.cpp.

References d.

00214 {
00215     Q_D(const QStackedLayout);
00216     return d->list.value(index);
00217 }

QLayoutItem * QStackedLayout::takeAt ( int  index  )  [virtual]

Implements QLayout.

Definition at line 235 of file qstackedlayout.cpp.

References d, emit, QWidget::hide(), qt_wasDeleted(), setCurrentIndex(), QLayoutItem::widget(), and widgetRemoved().

00236 {
00237     Q_D(QStackedLayout);
00238     if (index <0 || index >= d->list.size())
00239         return 0;
00240     QLayoutItem *item = d->list.takeAt(index);
00241     if (index == d->index) {
00242         d->index = -1;
00243         if ( d->list.count() > 0 ) {
00244             int newIndex = (index == d->list.count()) ? index-1 : index;
00245             setCurrentIndex(newIndex);
00246         }
00247     } else if (index < d->index) {
00248         --d->index;
00249     }
00250     emit widgetRemoved(index);
00251     if (item->widget() && !qt_wasDeleted(item->widget()))
00252         item->widget()->hide();
00253     return item;
00254 }

Here is the call graph for this function:

void QStackedLayout::setGeometry ( const QRect rect  )  [virtual]

Implements QLayout.

Definition at line 429 of file qstackedlayout.cpp.

References currentWidget(), QWidget::setGeometry(), and QLayoutItem::widget().

00430 {
00431     QWidget *widget = currentWidget();
00432     if (widget)
00433         widget->setGeometry(rect);
00434 }

Here is the call graph for this function:

void QStackedLayout::widgetRemoved ( int  index  )  [signal]

This signal is emitted whenever a widget is removed from the layout. The widget's index is passed as parameter.

See also:
removeWidget()

Referenced by takeAt().

void QStackedLayout::currentChanged ( int  index  )  [signal]

This signal is emitted whenever the current widget in the layout changes. The index specifies the index of the new current widget.

See also:
currentWidget(), setCurrentWidget()

Referenced by setCurrentIndex().

void QStackedLayout::setCurrentIndex ( int  index  )  [slot]

Definition at line 264 of file qstackedlayout.cpp.

References currentChanged(), currentWidget(), d, emit, QWidget::hide(), i, QWidget::isAncestorOf(), next, QObject::parent(), QLayout::parentWidget(), QWidget::setFocus(), Qt::TabFocus, and QLayoutItem::widget().

Referenced by insertWidget(), setCurrentWidget(), and takeAt().

00265 {
00266     Q_D(QStackedLayout);
00267     QWidget *prev = currentWidget();
00268     QWidget *next = widget(index);
00269     if (!next || next == prev)
00270         return;
00271 
00272     bool reenableUpdates = false;
00273     QWidget *parent = parentWidget();
00274 
00275     if (parent && parent->updatesEnabled()) {
00276         reenableUpdates = true;
00277         parent->setUpdatesEnabled(false);
00278     }
00279 
00280     d->index = index;
00281     next->raise();
00282     next->show();
00283 
00284     // try to move focus onto the incoming widget if focus
00285     // was somewhere on the outgoing widget.
00286 
00287     if (parent) {
00288         QWidget * fw = parent->window()->focusWidget();
00289         if (fw && (prev && prev->isAncestorOf(fw))) { // focus was on old page
00290             // look for the best focus widget we can find
00291             if (QWidget *nfw = next->focusWidget())
00292                 nfw->setFocus();
00293             else {
00294                 // second best: first child widget in the focus chain
00295                 QWidget *i = fw;
00296                 while ((i = i->nextInFocusChain()) != fw) {
00297                     if (((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus)
00298                         && !i->focusProxy() && i->isVisibleTo(next) && i->isEnabled()
00299                         && next->isAncestorOf(i)) {
00300                         i->setFocus();
00301                         break;
00302                     }
00303                 }
00304                 // third best: incoming widget
00305                 if (i == fw )
00306                     next->setFocus();
00307             }
00308         }
00309     }
00310     if (prev)
00311         prev->hide();
00312     if (reenableUpdates)
00313         parent->setUpdatesEnabled(true);
00314     emit currentChanged(index);
00315 }

void QStackedLayout::setCurrentWidget ( QWidget widget  )  [slot]

Sets the current widget to be the specified widget. The new current widget must already be contained in this stacked layout.

See also:
setCurrentIndex(), currentWidget()

Definition at line 332 of file qstackedlayout.cpp.

References index, QLayout::indexOf(), qWarning(), setCurrentIndex(), and QLayoutItem::widget().

00333 {
00334     int index = indexOf(widget);
00335   if (index == -1) {
00336     qWarning("QStackedLayout::setCurrentWidget: widget %p not contained in stack", widget);
00337     return;
00338   }
00339     setCurrentIndex(index);
00340 }


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 19:04:22 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1