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
src_gui_dialogs_qmessagebox.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 <QWidget>
5#include <QAbstractButton>
6#include <QPushButton>
7
8//! [4]
9#include <QApplication>
10#include <QMessageBox>
11
12int main(int argc, char *argv[])
13{
14 QT_REQUIRE_VERSION(argc, argv, "4.0.2")
15
16 QApplication app(argc, argv);
17 //...
18 return app.exec();
19}
20//! [4]
21
22class MyWidget : public QWidget
23{
25public:
27};
28
29void MyWidget::QMessageBoxExample(QWidget &myWidget)
30{
31 {
32 //! [0]
33 int ret = QMessageBox::warning(this, tr("My Application"),
34 tr("The document has been modified.\n"
35 "Do you want to save your changes?"),
36 QMessageBox::Save | QMessageBox::Discard
37 | QMessageBox::Cancel,
38 QMessageBox::Save);
39 //! [0]
40 }
41
42 {
43 //! [2]
44 QMessageBox msgBox(this);
45 QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole);
46 QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort);
47
48 msgBox.exec();
49
50 if (msgBox.clickedButton() == connectButton) {
51 // connect
52 } else if (msgBox.clickedButton() == abortButton) {
53 // abort
54 }
55 //! [2]
56 }
57
58 {
59 //! [3]
60 QMessageBox messageBox(this);
61 QAbstractButton *disconnectButton =
62 messageBox.addButton(tr("Disconnect"), QMessageBox::ActionRole);
63 //...
64 messageBox.exec();
65 if (messageBox.clickedButton() == disconnectButton) {
66 //...
67 }
68 //! [3]
69 }
70
71 {
72 //! [5]
73 QMessageBox msgBox(this);
74 msgBox.setText("The document has been modified.");
75 msgBox.exec();
76 //! [5]
77 }
78
79 {
80 //! [6]
81 QMessageBox msgBox(this);
82 msgBox.setText("The document has been modified.");
83 msgBox.setInformativeText("Do you want to save your changes?");
84 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
85 msgBox.setDefaultButton(QMessageBox::Save);
86 int ret = msgBox.exec();
87 //! [6]
88 }
89
90 {
91 int ret;
92
93 //! [7]
94 switch (ret) {
95 case QMessageBox::Save:
96 // Save was clicked
97 break;
98 case QMessageBox::Discard:
99 // Don't Save was clicked
100 break;
101 case QMessageBox::Cancel:
102 // Cancel was clicked
103 break;
104 default:
105 // should never be reached
106 break;
107 }
108 //! [7]
109 }
110}
int main(int argc, char *argv[])
[ctor_close]