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
qtcast.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4
#
include
<
QLabel
>
5
#
include
<
QPushButton
>
6
7
#
include
"qtcast.h"
8
9
MyWidget
::MyWidget()
10
{
11
//! [0]
12
QObject *obj =
new
MyWidget
;
13
//! [0]
14
15
//! [1]
16
QWidget *widget = qobject_cast<QWidget *>(obj);
17
//! [1]
18
19
//! [2]
20
MyWidget
*myWidget = qobject_cast<
MyWidget
*>(obj);
21
//! [2]
22
23
//! [3]
24
QLabel *label = qobject_cast<QLabel *>(obj);
25
//! [3] //! [4]
26
// label is nullptr
27
//! [4]
28
29
//! [5]
30
if
(QLabel *label = qobject_cast<QLabel *>(obj)) {
31
//! [5] //! [6]
32
label->setText(tr(
"Ping"
));
33
}
else
if
(QPushButton *button = qobject_cast<QPushButton *>(obj)) {
34
button->setText(tr(
"Pong!"
));
35
}
36
//! [6]
37
}
MyWidget
[24]
Definition
src_corelib_kernel_qobject.cpp:264
qtbase
src
corelib
doc
snippets
qtcast
qtcast.cpp
Generated on
for Qt by
1.18.0