1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qstackedwidget.h"
#include <qstackedlayout.h>
#include <qevent.h>
#include <private/qframe_p.h>
QT_BEGIN_NAMESPACE
class QStackedWidgetPrivate : public QFramePrivate
{
Q_DECLARE_PUBLIC(QStackedWidget)
public:
QStackedWidgetPrivate():layout(nullptr){}
QStackedLayout *layout;
};
/*!
\class QStackedWidget
\brief The QStackedWidget class provides a stack of widgets where
only one widget is visible at a time.
\ingroup organizers
\ingroup geomanagement
\inmodule QtWidgets
QStackedWidget can be used to create a user interface similar to
the one provided by QTabWidget. It is a convenience layout widget
built on top of the QStackedLayout class.
Like QStackedLayout, QStackedWidget can be constructed and
populated with a number of child widgets ("pages"):
\snippet qstackedwidget/main.cpp 0
\snippet qstackedwidget/main.cpp 2
\snippet qstackedwidget/main.cpp 3
QStackedWidget 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 QStackedWidget's pages. For
example:
\snippet qstackedwidget/main.cpp 1
When populating a stacked widget, 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 a widget from the stacked widget. The number of
widgets contained in the stacked widget 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 stacked widget changes or a
widget is removed from the stacked widget, the currentChanged()
and widgetRemoved() signals are emitted respectively.
\sa QStackedLayout, QTabWidget
*/
/*!
\fn void QStackedWidget::currentChanged(int index)
This signal is emitted whenever the current widget changes.
The parameter holds the \a index of the new current widget, or -1
if there isn't a new one (for example, if there are no widgets in
the QStackedWidget).
\sa currentWidget(), setCurrentWidget()
*/
/*!
\fn void QStackedWidget::widgetRemoved(int index)
This signal is emitted whenever a widget is removed. The widget's
\a index is passed as parameter.
\sa removeWidget()
*/
/*!
Constructs a QStackedWidget with the given \a parent.
\sa addWidget(), insertWidget()
*/
QStackedWidget::QStackedWidget(QWidget *parent)
: QFrame(*new QStackedWidgetPrivate, parent)
{
Q_D(QStackedWidget);
d->layout = new QStackedLayout(this);
connect(d->layout, SIGNAL(widgetRemoved(int)), this, SIGNAL(widgetRemoved(int)));
connect(d->layout, SIGNAL(currentChanged(int)), this, SIGNAL(currentChanged(int)));
}
/*!
Destroys this stacked widget, and frees any allocated resources.
*/
QStackedWidget::~QStackedWidget()
{
}
/*!
Appends the given \a widget to the QStackedWidget and returns the
index position. Ownership of \a widget is passed on to the
QStackedWidget.
If the QStackedWidget is empty before this function is called,
\a widget becomes the current widget.
\sa insertWidget(), removeWidget(), setCurrentWidget()
*/
int QStackedWidget::addWidget(QWidget *widget)
{
return d_func()->layout->addWidget(widget);
}
/*!
Inserts the given \a widget at the given \a index in the
QStackedWidget. Ownership of \a widget is passed on to the
QStackedWidget. If \a index is out of range, the \a widget is
appended (in which case it is the actual index of the \a widget
that is returned).
If the QStackedWidget was empty before this function is called,
the given \a 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.
\sa addWidget(), removeWidget(), setCurrentWidget()
*/
int QStackedWidget::insertWidget(int index, QWidget *widget)
{
return d_func()->layout->insertWidget(index, widget);
}
/*!
Removes \a widget from the QStackedWidget. i.e., \a widget is \e
not deleted but simply removed from the stacked layout, causing it
to be hidden.
\note Parent object and parent widget of \a widget will remain the
QStackedWidget. If the application wants to reuse the removed
\a widget, then it is recommended to re-parent it.
\sa addWidget(), insertWidget(), currentWidget()
*/
void QStackedWidget::removeWidget(QWidget *widget)
{
d_func()->layout->removeWidget(widget);
}
/*!
\property QStackedWidget::currentIndex
\brief the index position of the widget that is visible
The current index is -1 if there is no current widget.
By default, this property contains a value of -1 because the stack
is initially empty.
\sa currentWidget(), indexOf()
*/
void QStackedWidget::setCurrentIndex(int index)
{
d_func()->layout->setCurrentIndex(index);
}
int QStackedWidget::currentIndex() const
{
return d_func()->layout->currentIndex();
}
/*!
Returns the current widget, or \nullptr if there are no child widgets.
\sa currentIndex(), setCurrentWidget()
*/
QWidget *QStackedWidget::currentWidget() const
{
return d_func()->layout->currentWidget();
}
/*!
\fn void QStackedWidget::setCurrentWidget(QWidget *widget)
Sets the current widget to be the specified \a widget. The new
current widget must already be contained in this stacked widget.
\sa currentWidget(), setCurrentIndex()
*/
void QStackedWidget::setCurrentWidget(QWidget *widget)
{
Q_D(QStackedWidget);
if (Q_UNLIKELY(d->layout->indexOf(widget) == -1)) {
qWarning("QStackedWidget::setCurrentWidget: widget %p not contained in stack", widget);
return;
}
d->layout->setCurrentWidget(widget);
}
/*!
Returns the index of the given \a widget, or -1 if the given \a
widget is not a child of the QStackedWidget.
\sa currentIndex(), widget()
*/
int QStackedWidget::indexOf(const QWidget *widget) const
{
return d_func()->layout->indexOf(widget);
}
/*!
Returns the widget at the given \a index, or \nullptr if there is
no such widget.
\sa currentWidget(), indexOf()
*/
QWidget *QStackedWidget::widget(int index) const
{
return d_func()->layout->widget(index);
}
/*!
\property QStackedWidget::count
\brief the number of widgets contained by this stacked widget
By default, this property contains a value of 0.
\sa currentIndex(), widget()
*/
int QStackedWidget::count() const
{
return d_func()->layout->count();
}
/*! \reimp */
bool QStackedWidget::event(QEvent *e)
{
return QFrame::event(e);
}
QT_END_NAMESPACE
#include "moc_qstackedwidget.cpp"
|