Qt 4.8
qstackedwidget.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qstackedwidget.h"
43 
44 #ifndef QT_NO_STACKEDWIDGET
45 
46 #include <qstackedlayout.h>
47 #include <qevent.h>
48 #include <private/qframe_p.h>
49 
51 
60 {
61 public:
63  bool hasHeightForWidth() const;
64  int heightForWidth(int width) const;
65 };
66 
68 {
69  const int n = count();
70 
71  for (int i = 0; i < n; ++i) {
72  if (QLayoutItem *item = itemAt(i)) {
73  if (item->hasHeightForWidth())
74  return true;
75  }
76  }
77  return false;
78 }
79 
81 {
82  const int n = count();
83 
84  int hfw = 0;
85  for (int i = 0; i < n; ++i) {
86  if (QLayoutItem *item = itemAt(i)) {
87  if (QWidget *w = item->widget())
88  hfw = qMax(hfw, w->heightForWidth(width));
89  }
90  }
91 
92  hfw = qMax(hfw, minimumSize().height());
93  return hfw;
94 }
95 
96 
98 {
100 public:
104 };
105 
192  : QFrame(*new QStackedWidgetPrivate, parent)
193 {
195  d->layout = new QStackedLayoutHFW(this);
196  connect(d->layout, SIGNAL(widgetRemoved(int)), this, SIGNAL(widgetRemoved(int)));
197  connect(d->layout, SIGNAL(currentChanged(int)), this, SIGNAL(currentChanged(int)));
198 }
199 
204 {
205 }
206 
218 {
219  return d_func()->layout->addWidget(widget);
220 }
221 
238 {
239  return d_func()->layout->insertWidget(index, widget);
240 }
241 
252 {
253  d_func()->layout->removeWidget(widget);
254 }
255 
272 {
273  d_func()->layout->setCurrentIndex(index);
274 }
275 
277 {
278  return d_func()->layout->currentIndex();
279 }
280 
287 {
288  return d_func()->layout->currentWidget();
289 }
290 
291 
301 {
303  if (d->layout->indexOf(widget) == -1) {
304  qWarning("QStackedWidget::setCurrentWidget: widget %p not contained in stack", widget);
305  return;
306  }
307  d->layout->setCurrentWidget(widget);
308 }
309 
317 {
318  return d_func()->layout->indexOf(widget);
319 }
320 
328 {
329  return d_func()->layout->widget(index);
330 }
331 
343 int QStackedWidget::count() const
344 {
345  return d_func()->layout->count();
346 }
347 
350 {
351  return QFrame::event(e);
352 }
353 
355 
356 #endif // QT_NO_STACKEDWIDGET
double d
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void removeWidget(QWidget *w)
Removes widget from the QStackedWidget.
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
~QStackedWidget()
Destroys this stacked widget, and frees any allocated resources.
int heightForWidth(int width) const
Returns the preferred height for this layout item, given the width w.
#define Q_D(Class)
Definition: qglobal.h:2482
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
The QStackedLayout class provides a stack of widgets where only one widget is visible at a time...
QStackedLayoutHFW * layout
QWidget * widget(int) const
Returns the widget at the given index, or 0 if there is no such widget.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
int insertWidget(int index, QWidget *w)
Inserts the given widget at the given index in the QStackedWidget.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void widgetRemoved(int index)
This signal is emitted whenever a widget is removed.
The QLayoutItem class provides an abstract item that a QLayout manipulates.
Definition: qlayoutitem.h:64
int count() const
the number of widgets contained in the layout
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 rece...
Definition: qobject.cpp:2580
bool hasHeightForWidth() const
Returns true if this layout&#39;s preferred height depends on its width; otherwise returns false...
int addWidget(QWidget *w)
Appends the given widget to the QStackedWidget and returns the index position.
Q_CORE_EXPORT void qWarning(const char *,...)
bool event(QEvent *e)
Reimplemented Function
void setCurrentIndex(int index)
int count() const
QStackedWidget(QWidget *parent=0)
Constructs a QStackedWidget with the given parent.
QWidget * currentWidget() const
Returns the current widget, or 0 if there are no child widgets.
QSize minimumSize() const
Reimplemented Function
void setCurrentWidget(QWidget *w)
Sets the current widget to be the specified widget.
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
bool event(QEvent *e)
Reimplemented Function
Definition: qframe.cpp:587
int currentIndex() const
int indexOf(QWidget *) const
Returns the index of the given widget, or -1 if the given widget is not a child of the QStackedWidget...
void currentChanged(int)
This signal is emitted whenever the current widget changes.
quint16 index
virtual QWidget * widget()
If this item is a QWidget, it is returned as a QWidget; otherwise 0 is returned.
QLayout * layout() const
Returns the layout manager that is installed on this widget, or 0 if no layout manager is installed...
Definition: qwidget.cpp:10073
QLayout * layout()
Reimplemented Function
QStackedLayoutHFW(QWidget *parent=0)
The QStackedWidget class provides a stack of widgets where only one widget is visible at a time...
QLayoutItem * itemAt(int) const
Reimplemented Function
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
The QFrame class is the base class of widgets that can have a frame.
Definition: qframe.h:55