Qt 4.8
Public Types | Public Slots | Signals | Public Functions | Properties | List of all members
QStackedLayout Class Reference

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

#include <qstackedlayout.h>

Inheritance diagram for QStackedLayout:
QLayout QObject QLayoutItem QStackedLayoutHFW

Public Types

enum  StackingMode { StackOne, StackAll }
 This enum specifies how the layout handles its child widgets regarding their visibility. More...
 
- Public Types inherited from QLayout
enum  SizeConstraint {
  SetDefaultConstraint, SetNoConstraint, SetMinimumSize, SetFixedSize,
  SetMaximumSize, SetMinAndMaxSize
}
 The possible values are: More...
 

Public Slots

void setCurrentIndex (int index)
 
void setCurrentWidget (QWidget *w)
 Sets the current widget to be the specified widget. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 

Signals

void currentChanged (int index)
 This signal is emitted whenever the current widget in the layout changes. More...
 
void widgetRemoved (int index)
 This signal is emitted whenever a widget is removed from the layout. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 

Public Functions

void addItem (QLayoutItem *item)
 Reimplemented Function More...
 
int addWidget (QWidget *w)
 Adds the given widget to the end of this layout and returns the index position of the widget. More...
 
int count () const
 the number of widgets contained in the layout More...
 
int currentIndex () const
 
QWidgetcurrentWidget () const
 Returns the current widget, or 0 if there are no widgets in this layout. More...
 
int insertWidget (int index, QWidget *w)
 Inserts the given widget at the given index in this QStackedLayout. More...
 
QLayoutItemitemAt (int) const
 Reimplemented Function More...
 
QSize minimumSize () const
 Reimplemented Function More...
 
 QStackedLayout ()
 Constructs a QStackedLayout with no parent. More...
 
 QStackedLayout (QWidget *parent)
 Constructs a new QStackedLayout with the given parent. More...
 
 QStackedLayout (QLayout *parentLayout)
 Constructs a new QStackedLayout and inserts it into the given parentLayout. More...
 
void setGeometry (const QRect &rect)
 Reimplemented Function More...
 
void setStackingMode (StackingMode stackingMode)
 
QSize sizeHint () const
 Reimplemented Function More...
 
StackingMode stackingMode () const
 
QLayoutItemtakeAt (int)
 Reimplemented Function More...
 
QWidgetwidget (int) const
 Returns the widget at the given index, or 0 if there is no widget at the given position. More...
 
 ~QStackedLayout ()
 Destroys this QStackedLayout. More...
 
- Public Functions inherited from QLayout
bool activate ()
 Redoes the layout for parentWidget() if necessary. More...
 
void addWidget (QWidget *w)
 Adds widget w to this layout in a manner specific to the layout. More...
 
QMargins contentsMargins () const
 Returns the margins used around the layout. More...
 
QRect contentsRect () const
 Returns the layout's geometry() rectangle, but taking into account the contents margins. More...
 
Qt::Orientations expandingDirections () const
 Returns whether this layout can make use of more space than sizeHint(). More...
 
QRect geometry () const
 Reimplemented Function More...
 
void getContentsMargins (int *left, int *top, int *right, int *bottom) const
 
virtual int indexOf (QWidget *) const
 Searches for widget widget in this layout (not including child layouts). More...
 
void invalidate ()
 Reimplemented Function More...
 
bool isEmpty () const
 Reimplemented Function More...
 
bool isEnabled () const
 Returns true if the layout is enabled; otherwise returns false. More...
 
QLayoutlayout ()
 Reimplemented Function More...
 
int margin () const
 
QSize maximumSize () const
 Returns the maximum size of this layout. More...
 
QWidgetmenuBar () const
 Returns the menu bar set for this layout, or 0 if no menu bar is set. More...
 
QWidgetparentWidget () const
 Returns the parent widget of this layout, or 0 if this layout is not installed on any widget. More...
 
 QLayout (QWidget *parent)
 Constructs a new top-level QLayout, with parent parent. More...
 
 QLayout ()
 Constructs a new child QLayout. More...
 
void removeItem (QLayoutItem *)
 Removes the layout item item from the layout. More...
 
void removeWidget (QWidget *w)
 Removes the widget widget from the layout. More...
 
bool setAlignment (QWidget *w, Qt::Alignment alignment)
 Sets the alignment for widget w to alignment and returns true if w is found in this layout (not including child layouts); otherwise returns false. More...
 
bool setAlignment (QLayout *l, Qt::Alignment alignment)
 Sets the alignment for the layout l to alignment and returns true if l is found in this layout (not including child layouts); otherwise returns false. More...
 
void setContentsMargins (int left, int top, int right, int bottom)
 Sets the left, top, right, and bottom margins to use around the layout. More...
 
void setContentsMargins (const QMargins &margins)
 Sets the margins to use around the layout. More...
 
void setEnabled (bool)
 Enables this layout if enable is true, otherwise disables it. More...
 
void setMargin (int)
 
void setMenuBar (QWidget *w)
 Tells the geometry manager to place the menu bar widget at the top of parentWidget(), outside QWidget::contentsMargins(). More...
 
void setSizeConstraint (SizeConstraint)
 
void setSpacing (int)
 
SizeConstraint sizeConstraint () const
 
int spacing () const
 
int totalHeightForWidth (int w) const
 Also takes contentsMargins and menu bar into account. More...
 
QSize totalMaximumSize () const
 Also takes contentsMargins and menu bar into account. More...
 
QSize totalMinimumSize () const
 Also takes contentsMargins and menu bar into account. More...
 
QSize totalSizeHint () const
 Also takes contentsMargins and menu bar into account. More...
 
void update ()
 Updates the layout for parentWidget(). More...
 
 ~QLayout ()
 Destroys the layout, deleting all child layouts. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 
- Public Functions inherited from QLayoutItem
Qt::Alignment alignment () const
 Returns the alignment of this item. More...
 
QSizePolicy::ControlTypes controlTypes () const
 Returns the control type(s) for the layout item. More...
 
virtual bool hasHeightForWidth () const
 Returns true if this layout's preferred height depends on its width; otherwise returns false. More...
 
virtual int heightForWidth (int) const
 Returns the preferred height for this layout item, given the width w. More...
 
virtual int minimumHeightForWidth (int) const
 Returns the minimum height this widget needs for the given width, w. More...
 
 QLayoutItem (Qt::Alignment alignment=0)
 Constructs a layout item with an alignment. More...
 
void setAlignment (Qt::Alignment a)
 Sets the alignment of this item to alignment. More...
 
virtual QSpacerItemspacerItem ()
 If this item is a QSpacerItem, it is returned as a QSpacerItem; otherwise 0 is returned. More...
 
virtual QWidgetwidget ()
 If this item is a QWidget, it is returned as a QWidget; otherwise 0 is returned. More...
 
virtual ~QLayoutItem ()
 Destroys the QLayoutItem. More...
 

Properties

int currentIndex
 the index position of the widget that is visible More...
 
StackingMode stackingMode
 determines the way visibility of child widgets are handled. More...
 

Additional Inherited Members

- Static Public Functions inherited from QLayout
static QSize closestAcceptableSize (const QWidget *w, const QSize &s)
 Returns a size that satisfies all size constraints on widget, including heightForWidth() and that is as close as possible to size. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QLayout
void addChildLayout (QLayout *l)
 This function is called from addLayout() or insertLayout() functions in subclasses to add layout l as a sub-layout. More...
 
void addChildWidget (QWidget *w)
 This function is called from addWidget() functions in subclasses to add w as a managed widget of a layout. More...
 
bool adoptLayout (QLayout *layout)
 
QRect alignmentRect (const QRect &) const
 Returns the rectangle that should be covered when the geometry of this layout is set to r, provided that this layout supports setAlignment(). More...
 
void childEvent (QChildEvent *e)
 Reimplemented Function More...
 
 QLayout (QLayoutPrivate &d, QLayout *, QWidget *)
 
void widgetEvent (QEvent *)
 Performs child widget layout when the parent widget is resized. More...
 
- Protected Functions inherited from QObject
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Protected Variables inherited from QLayoutItem
Qt::Alignment align
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

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:

QWidget *firstPageWidget = new QWidget;
QWidget *secondPageWidget = new QWidget;
QWidget *thirdPageWidget = new QWidget;
QStackedLayout *stackedLayout = new QStackedLayout;
stackedLayout->addWidget(firstPageWidget);
stackedLayout->addWidget(secondPageWidget);
stackedLayout->addWidget(thirdPageWidget);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(stackedLayout);
setLayout(mainLayout);

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:

QComboBox *pageComboBox = new QComboBox;
pageComboBox->addItem(tr("Page 1"));
pageComboBox->addItem(tr("Page 2"));
pageComboBox->addItem(tr("Page 3"));
connect(pageComboBox, SIGNAL(activated(int)),
stackedLayout, SLOT(setCurrentIndex(int)));

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 55 of file qstackedlayout.h.

Enumerations

◆ StackingMode

This enum specifies how the layout handles its child widgets regarding their visibility.

Since
4.4
  • StackOne Only the current widget is visible. This is the default.
  • StackAll All widgets are visible. The current widget is merely raised.
Enumerator
StackOne 
StackAll 

Definition at line 65 of file qstackedlayout.h.

Constructors and Destructors

◆ QStackedLayout() [1/3]

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 153 of file qstackedlayout.cpp.

154  : QLayout(*new QStackedLayoutPrivate, 0, 0)
155 {
156 }
QLayout()
Constructs a new child QLayout.
Definition: qlayout.cpp:127

◆ QStackedLayout() [2/3]

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 164 of file qstackedlayout.cpp.

165  : QLayout(*new QStackedLayoutPrivate, 0, parent)
166 {
167 }
QLayout()
Constructs a new child QLayout.
Definition: qlayout.cpp:127

◆ QStackedLayout() [3/3]

QStackedLayout::QStackedLayout ( QLayout parentLayout)
explicit

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

Definition at line 173 of file qstackedlayout.cpp.

174  : QLayout(*new QStackedLayoutPrivate, parentLayout, 0)
175 {
176 }
QLayout()
Constructs a new child QLayout.
Definition: qlayout.cpp:127

◆ ~QStackedLayout()

QStackedLayout::~QStackedLayout ( )

Destroys this QStackedLayout.

Note that the layout's widgets are not destroyed.

Definition at line 182 of file qstackedlayout.cpp.

183 {
185  qDeleteAll(d->list);
186 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319

Functions

◆ addItem()

void QStackedLayout::addItem ( QLayoutItem item)
virtual

Reimplemented Function

Implements QLayout.

Definition at line 429 of file qstackedlayout.cpp.

430 {
431  QWidget *widget = item->widget();
432  if (widget) {
433  addWidget(widget);
434  delete item;
435  } else {
436  qWarning("QStackedLayout::addItem: Only widgets can be added");
437  }
438 }
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
int addWidget(QWidget *w)
Adds the given widget to the end of this layout and returns the index position of the widget...
Q_CORE_EXPORT void qWarning(const char *,...)
virtual QWidget * widget()
If this item is a QWidget, it is returned as a QWidget; otherwise 0 is returned.

◆ addWidget()

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()

Definition at line 197 of file qstackedlayout.cpp.

Referenced by addItem().

198 {
200  return insertWidget(d->list.count(), widget);
201 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
int insertWidget(int index, QWidget *w)
Inserts the given widget at the given index in this QStackedLayout.
virtual QWidget * widget()
If this item is a QWidget, it is returned as a QWidget; otherwise 0 is returned.

◆ count()

int QStackedLayout::count ( ) const
virtual

the number of widgets contained in the layout

See also
currentIndex(), widget()

Implements QLayout.

Definition at line 419 of file qstackedlayout.cpp.

Referenced by QStackedLayoutHFW::hasHeightForWidth(), and QStackedLayoutHFW::heightForWidth().

420 {
421  Q_D(const QStackedLayout);
422  return d->list.size();
423 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...

◆ currentChanged

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, or -1 if there isn't a new one (for example, if there are no widgets in the QStackedLayout)

See also
currentWidget(), setCurrentWidget()

Referenced by setCurrentIndex(), and takeAt().

◆ currentIndex()

int QStackedLayout::currentIndex ( ) const

Referenced by setCurrentIndex(), and setStackingMode().

◆ currentWidget()

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 390 of file qstackedlayout.cpp.

Referenced by setCurrentIndex(), setGeometry(), and setStackingMode().

391 {
392  Q_D(const QStackedLayout);
393  return d->index >= 0 ? d->list.at(d->index)->widget() : 0;
394 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...

◆ insertWidget()

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 217 of file qstackedlayout.cpp.

Referenced by addWidget().

218 {
220  addChildWidget(widget);
221  index = qMin(index, d->list.count());
222  if (index < 0)
223  index = d->list.count();
224  QWidgetItem *wi = QLayoutPrivate::createWidgetItem(this, widget);
225  d->list.insert(index, wi);
226  invalidate();
227  if (d->index < 0) {
229  } else {
230  if (index <= d->index)
231  ++d->index;
232  if (d->stackingMode == StackOne)
233  widget->hide();
234  widget->lower();
235  }
236  return index;
237 }
double d
Definition: qnumeric_p.h:62
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
void setCurrentIndex(int index)
void invalidate()
Reimplemented Function
Definition: qlayout.cpp:673
#define Q_D(Class)
Definition: qglobal.h:2482
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
void lower()
Lowers the widget to the bottom of the parent widget&#39;s stack.
Definition: qwidget.cpp:11939
The QWidgetItem class is a layout item that represents a widget.
Definition: qlayoutitem.h:122
void hide()
Hides the widget.
Definition: qwidget.h:501
static QWidgetItem * createWidgetItem(const QLayout *layout, QWidget *widget)
Definition: qlayout.cpp:190
void addChildWidget(QWidget *w)
This function is called from addWidget() functions in subclasses to add w as a managed widget of a la...
Definition: qlayout.cpp:1045
quint16 index

◆ itemAt()

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

Reimplemented Function

Implements QLayout.

Definition at line 242 of file qstackedlayout.cpp.

Referenced by QStackedLayoutHFW::hasHeightForWidth(), and QStackedLayoutHFW::heightForWidth().

243 {
244  Q_D(const QStackedLayout);
245  return d->list.value(index);
246 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
quint16 index

◆ minimumSize()

QSize QStackedLayout::minimumSize ( ) const
virtual

Reimplemented Function

Reimplemented from QLayout.

Definition at line 464 of file qstackedlayout.cpp.

Referenced by QStackedLayoutHFW::heightForWidth().

465 {
466  Q_D(const QStackedLayout);
467  QSize s(0, 0);
468  int n = d->list.count();
469 
470  for (int i = 0; i < n; ++i)
471  if (QWidget *widget = d->list.at(i)->widget())
472  s = s.expandedTo(qSmartMinSize(widget));
473  return s;
474 }
double d
Definition: qnumeric_p.h:62
Q_GUI_EXPORT QSize qSmartMinSize(const QSize &sizeHint, const QSize &minSizeHint, const QSize &minSize, const QSize &maxSize, const QSizePolicy &sizePolicy)
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
#define Q_D(Class)
Definition: qglobal.h:2482
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
virtual QWidget * widget()
If this item is a QWidget, it is returned as a QWidget; otherwise 0 is returned.
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53

◆ setCurrentIndex

void QStackedLayout::setCurrentIndex ( int  index)
slot

Definition at line 298 of file qstackedlayout.cpp.

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

299 {
301  QWidget *prev = currentWidget();
302  QWidget *next = widget(index);
303  if (!next || next == prev)
304  return;
305 
306  bool reenableUpdates = false;
308 
309  if (parent && parent->updatesEnabled()) {
310  reenableUpdates = true;
311  parent->setUpdatesEnabled(false);
312  }
313 
314  QPointer<QWidget> fw = parent ? parent->window()->focusWidget() : 0;
315  const bool focusWasOnOldPage = fw && (prev && prev->isAncestorOf(fw));
316 
317  if (prev) {
318  prev->clearFocus();
319  if (d->stackingMode == StackOne)
320  prev->hide();
321  }
322 
323  d->index = index;
324  next->raise();
325  next->show();
326 
327  // try to move focus onto the incoming widget if focus
328  // was somewhere on the outgoing widget.
329 
330  if (parent) {
331  if (focusWasOnOldPage) {
332  // look for the best focus widget we can find
333  if (QWidget *nfw = next->focusWidget())
334  nfw->setFocus();
335  else {
336  // second best: first child widget in the focus chain
337  if (QWidget *i = fw) {
338  while ((i = i->nextInFocusChain()) != fw) {
339  if (((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus)
340  && !i->focusProxy() && i->isVisibleTo(next) && i->isEnabled()
341  && next->isAncestorOf(i)) {
342  i->setFocus();
343  break;
344  }
345  }
346  // third best: incoming widget
347  if (i == fw )
348  next->setFocus();
349  }
350  }
351  }
352  }
353  if (reenableUpdates)
354  parent->setUpdatesEnabled(true);
356 }
double d
Definition: qnumeric_p.h:62
QWidget * focusWidget() const
Returns the last child of this widget that setFocus had been called on.
Definition: qwidget.cpp:6863
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void setUpdatesEnabled(bool enable)
Definition: qwidget.cpp:7670
#define Q_D(Class)
Definition: qglobal.h:2482
void currentChanged(int index)
This signal is emitted whenever the current widget in the layout changes.
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
QWidget * parentWidget() const
Returns the parent widget of this layout, or 0 if this layout is not installed on any widget...
Definition: qlayout.cpp:616
void clearFocus()
Takes keyboard input focus from the widget.
Definition: qwidget.cpp:6757
bool updatesEnabled
whether updates are enabled
Definition: qwidget.h:190
void raise()
Raises this widget to the top of the parent widget&#39;s stack.
Definition: qwidget.cpp:11901
#define emit
Definition: qobjectdefs.h:76
void show()
Shows the widget and its child widgets.
void hide()
Hides the widget.
Definition: qwidget.h:501
void setFocus()
Gives the keyboard input focus to this widget (or its focus proxy) if this widget or one of its paren...
Definition: qwidget.h:432
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
bool isAncestorOf(const QWidget *child) const
Returns true if this widget is a parent, (or grandparent and so on to any level), of the given child...
Definition: qwidget.cpp:8573
quint16 index
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
virtual QWidget * widget()
If this item is a QWidget, it is returned as a QWidget; otherwise 0 is returned.
QWidget * currentWidget() const
Returns the current widget, or 0 if there are no widgets in this layout.

◆ setCurrentWidget

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 373 of file qstackedlayout.cpp.

374 {
375  int index = indexOf(widget);
376  if (index == -1) {
377  qWarning("QStackedLayout::setCurrentWidget: Widget %p not contained in stack", widget);
378  return;
379  }
380  setCurrentIndex(index);
381 }
void setCurrentIndex(int index)
virtual int indexOf(QWidget *) const
Searches for widget widget in this layout (not including child layouts).
Definition: qlayout.cpp:1378
Q_CORE_EXPORT void qWarning(const char *,...)
quint16 index

◆ setGeometry()

void QStackedLayout::setGeometry ( const QRect rect)
virtual

Reimplemented Function

Reimplemented from QLayout.

Definition at line 479 of file qstackedlayout.cpp.

480 {
482  switch (d->stackingMode) {
483  case StackOne:
484  if (QWidget *widget = currentWidget())
485  widget->setGeometry(rect);
486  break;
487  case StackAll:
488  if (const int n = d->list.count())
489  for (int i = 0; i < n; ++i)
490  if (QWidget *widget = d->list.at(i)->widget())
491  widget->setGeometry(rect);
492  break;
493  }
494 }
double d
Definition: qnumeric_p.h:62
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qwidget.h:1017
#define Q_D(Class)
Definition: qglobal.h:2482
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
virtual QWidget * widget()
If this item is a QWidget, it is returned as a QWidget; otherwise 0 is returned.
QWidget * currentWidget() const
Returns the current widget, or 0 if there are no widgets in this layout.

◆ setStackingMode()

void QStackedLayout::setStackingMode ( StackingMode  stackingMode)

Definition at line 534 of file qstackedlayout.cpp.

535 {
537  if (d->stackingMode == stackingMode)
538  return;
539  d->stackingMode = stackingMode;
540 
541  const int n = d->list.count();
542  if (n == 0)
543  return;
544 
545  switch (d->stackingMode) {
546  case StackOne:
547  if (const int idx = currentIndex())
548  for (int i = 0; i < n; ++i)
549  if (QWidget *widget = d->list.at(i)->widget())
550  widget->setVisible(i == idx);
551  break;
552  case StackAll: { // Turn overlay on: Make sure all widgets are the same size
553  QRect geometry;
554  if (const QWidget *widget = currentWidget())
555  geometry = widget->geometry();
556  for (int i = 0; i < n; ++i)
557  if (QWidget *widget = d->list.at(i)->widget()) {
558  if (!geometry.isNull())
559  widget->setGeometry(geometry);
560  widget->setVisible(true);
561  }
562  }
563  break;
564  }
565 }
double d
Definition: qnumeric_p.h:62
bool isNull() const
Returns true if the rectangle is a null rectangle, otherwise returns false.
Definition: qrect.h:231
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qwidget.h:1017
#define Q_D(Class)
Definition: qglobal.h:2482
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
int currentIndex() const
StackingMode stackingMode() const
virtual void setVisible(bool visible)
Definition: qwidget.cpp:7991
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
virtual QWidget * widget()
If this item is a QWidget, it is returned as a QWidget; otherwise 0 is returned.
QRect geometry() const
Reimplemented Function
Definition: qlayout.cpp:664
QRect geometry
the geometry of the widget relative to its parent and excluding the window frame
Definition: qwidget.h:158
QWidget * currentWidget() const
Returns the current widget, or 0 if there are no widgets in this layout.

◆ sizeHint()

QSize QStackedLayout::sizeHint ( ) const
virtual

Reimplemented Function

Implements QLayoutItem.

Definition at line 443 of file qstackedlayout.cpp.

444 {
445  Q_D(const QStackedLayout);
446  QSize s(0, 0);
447  int n = d->list.count();
448 
449  for (int i = 0; i < n; ++i)
450  if (QWidget *widget = d->list.at(i)->widget()) {
451  QSize ws(widget->sizeHint());
453  ws.setWidth(0);
455  ws.setHeight(0);
456  s = s.expandedTo(ws);
457  }
458  return s;
459 }
double d
Definition: qnumeric_p.h:62
Q_CORE_EXPORT QTextStream & ws(QTextStream &s)
Policy horizontalPolicy() const
Definition: qsizepolicy.h:118
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
#define Q_D(Class)
Definition: qglobal.h:2482
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
QSizePolicy sizePolicy
the default layout behavior of the widget
Definition: qwidget.h:171
Policy verticalPolicy() const
Definition: qsizepolicy.h:119
virtual QWidget * widget()
If this item is a QWidget, it is returned as a QWidget; otherwise 0 is returned.
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QSize sizeHint
the recommended size for the widget
Definition: qwidget.h:195

◆ stackingMode()

StackingMode QStackedLayout::stackingMode ( ) const

Referenced by setGeometry(), and setStackingMode().

◆ takeAt()

QLayoutItem * QStackedLayout::takeAt ( int  index)
virtual

Reimplemented Function

Implements QLayout.

Definition at line 264 of file qstackedlayout.cpp.

265 {
267  if (index <0 || index >= d->list.size())
268  return 0;
269  QLayoutItem *item = d->list.takeAt(index);
270  if (index == d->index) {
271  d->index = -1;
272  if ( d->list.count() > 0 ) {
273  int newIndex = (index == d->list.count()) ? index-1 : index;
274  setCurrentIndex(newIndex);
275  } else {
276  emit currentChanged(-1);
277  }
278  } else if (index < d->index) {
279  --d->index;
280  }
281  emit widgetRemoved(index);
282  if (item->widget() && !qt_wasDeleted(item->widget()))
283  item->widget()->hide();
284  return item;
285 }
double d
Definition: qnumeric_p.h:62
static bool qt_wasDeleted(const QWidget *w)
void setCurrentIndex(int index)
void widgetRemoved(int index)
This signal is emitted whenever a widget is removed from the layout.
#define Q_D(Class)
Definition: qglobal.h:2482
void currentChanged(int index)
This signal is emitted whenever the current widget in the layout changes.
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
The QLayoutItem class provides an abstract item that a QLayout manipulates.
Definition: qlayoutitem.h:64
#define emit
Definition: qobjectdefs.h:76
void hide()
Hides the widget.
Definition: qwidget.h:501
quint16 index
virtual QWidget * widget()
If this item is a QWidget, it is returned as a QWidget; otherwise 0 is returned.

◆ widget()

QWidget * QStackedLayout::widget ( int  index) const

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

Warning
This function is not part of the public interface.
See also
currentWidget(), indexOf()

Definition at line 402 of file qstackedlayout.cpp.

403 {
404  Q_D(const QStackedLayout);
405  if (index < 0 || index >= d->list.size())
406  return 0;
407  return d->list.at(index)->widget();
408 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
quint16 index

◆ widgetRemoved

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().

Properties

◆ currentIndex

int QStackedLayout::currentIndex
private

the index position of the widget that is visible

The current index is -1 if there is no current widget.

See also
currentWidget(), indexOf()

Definition at line 60 of file qstackedlayout.h.

◆ stackingMode

QStackedLayout::StackingMode QStackedLayout::stackingMode
private

determines the way visibility of child widgets are handled.

Since
4.4

The default value is StackOne. Setting the property to StackAll allows you to make use of the layout for overlay widgets that do additional drawing on top of other widgets, for example, graphical editors.

Definition at line 61 of file qstackedlayout.h.


The documentation for this class was generated from the following files: