Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
9MyWidget::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}