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