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
dialogs.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
<
QtWidgets
>
5
6
typedef
QDialog
WordCountDialog
;
7
8
#
define
this
0
9
#
define
setWordCount
(
x
)
isVisible
(
)
10
11
QString
tr
(
const
char
*text)
12
{
13
return
QApplication::translate(text, text);
14
}
15
16
class
FindDialog
:
public
QDialog
17
{
18
Q_OBJECT
19
public
:
20
FindDialog
(
QWidget
*
parent
=
nullptr
)
21
:
QDialog
(
parent
) {}
22
23
signals
:
24
void
findNext
();
25
};
26
27
class
EditorWindow
:
public
QMainWindow
28
{
29
public
:
30
void
find
();
31
void
countWords
();
32
void
findNext
();
33
34
private
:
35
FindDialog
*findDialog;
36
};
37
38
//! [0]
39
void
EditorWindow
::
find
()
40
{
41
if
(!findDialog) {
42
findDialog =
new
FindDialog(
this
);
43
connect(findDialog, &FindDialog::findNext,
44
this
, &
EditorWindow
::
findNext
);
45
}
46
47
findDialog->show();
48
findDialog->raise();
49
findDialog->activateWindow();
50
}
51
//! [0]
52
53
//! [1]
54
void
EditorWindow
::
countWords
()
55
{
56
WordCountDialog
dialog(
this
);
57
dialog.
setWordCount
(document().wordCount());
58
dialog.exec();
59
}
60
//! [1]
61
62
#
if
QT_DEPRECATED_SINCE
(
6
,
2
)
63
inline
bool
boo()
64
{
65
QMessageBox::information(
this
,
"Application name"
,
66
"Unable to find the user preferences file.\n"
67
"The factory default will be used instead."
);
68
69
QString filename;
70
if
(QFile::exists(filename) &&
71
QMessageBox::question(
72
this
,
73
tr(
"Overwrite File? -- Application Name"
),
74
tr(
"A file called %1 already exists."
75
"Do you want to overwrite it?"
)
76
.arg(filename),
77
tr(
"&Yes"
), tr(
"&No"
),
78
QString(), 0, 1))
79
return
false
;
80
81
switch
(QMessageBox::warning(
this
,
"Application name"
,
82
"Could not connect to the <mumble> server.\n"
83
"This program can't function correctly "
84
"without the server.\n\n"
,
85
"Retry"
,
86
"Quit"
, 0, 0, 1)) {
87
case
0:
// The user clicked the Retry again button or pressed Enter
88
// try again
89
break
;
90
case
1:
// The user clicked the Quit or pressed Escape
91
// exit
92
break
;
93
}
94
95
switch
(QMessageBox::information(
this
,
"Application name here"
,
96
"The document contains unsaved changes\n"
97
"Do you want to save the changes before exiting?"
,
98
"&Save"
,
"&Discard"
,
"Cancel"
,
99
0,
// Enter == button 0
100
2)) {
// Escape == button 2
101
case
0:
// Save clicked or Alt+S pressed or Enter pressed.
102
// save
103
break
;
104
case
1:
// Discard clicked or Alt+D pressed
105
// don't save but exit
106
break
;
107
case
2:
// Cancel clicked or Escape pressed
108
// don't exit
109
break
;
110
}
111
112
switch
(QMessageBox::warning(
this
,
"Application name here"
,
113
"Could not save the user preferences,\n"
114
"because the disk is full. You can delete\n"
115
"some files and press Retry, or you can\n"
116
"abort the Save Preferences operation."
,
117
QMessageBox::Retry | QMessageBox::Default,
118
QMessageBox::Abort | QMessageBox::Escape)) {
119
case
QMessageBox::Retry:
// Retry clicked or Enter pressed
120
// try again
121
break
;
122
case
QMessageBox::Abort:
// Abort clicked or Escape pressed
123
// abort
124
break
;
125
}
126
127
QString errorDetails;
128
QMessageBox::critical(0,
"Application name here"
,
129
QString(
"An internal error occurred. Please "
) +
130
"call technical support at 1234-56789 and report\n"
+
131
"these numbers:\n\n"
+ errorDetails +
132
"\n\nApplication will now exit."
);
133
134
QMessageBox::about(
this
,
"About <Application>"
,
135
"<Application> is a <one-paragraph blurb>\n\n"
136
"Copyright 1991-2003 Such-and-such. "
137
"<License words here.>\n\n"
138
"For technical support, call 1234-56789 or see\n"
139
"http://www.such-and-such.com/Application/\n"
);
140
141
{
142
// saving the file
143
QMessageBox mb(
"Application name here"
,
144
"Saving the file will overwrite the original file on the disk.\n"
145
"Do you really want to save?"
,
146
QMessageBox::Information,
147
QMessageBox::Yes | QMessageBox::Default,
148
QMessageBox::No,
149
QMessageBox::Cancel | QMessageBox::Escape);
150
mb.setButtonText(QMessageBox::Yes,
"Save"
);
151
mb.setButtonText(QMessageBox::No,
"Discard"
);
152
switch
(mb.exec()) {
153
case
QMessageBox::Yes:
154
// save and exit
155
break
;
156
case
QMessageBox::No:
157
// exit without saving
158
break
;
159
case
QMessageBox::Cancel:
160
// don't save and don't exit
161
break
;
162
}
163
}
164
165
{
166
// hardware failure
167
//! [2]
168
QMessageBox mb(
"Application Name"
,
169
"Hardware failure.\n\nDisk error detected\nDo you want to stop?"
,
170
QMessageBox::Question,
171
QMessageBox::Yes | QMessageBox::Default,
172
QMessageBox::No | QMessageBox::Escape,
173
QMessageBox::NoButton);
174
if
(mb.exec() == QMessageBox::No) {
175
// try again
176
//! [2]
177
}
178
}
179
}
180
#
endif
// QT_DEPRECATED_SINCE(6, 2)
181
182
inline
void
moo
()
183
{
184
int
numFiles;
185
//! [3]
186
QProgressDialog progress(
"Copying files..."
,
"Abort Copy"
, 0, numFiles,
this
);
187
progress.setWindowModality(Qt::WindowModal);
188
189
for
(
int
i = 0; i < numFiles; i++) {
190
progress.setValue(i);
191
192
if
(progress.wasCanceled())
193
break
;
194
//... copy one file
195
}
196
progress.setValue(numFiles);
197
//! [3]
198
}
199
200
class
Operation
:
public
QObject
201
{
202
public
:
203
Operation
(QObject *parent);
204
void
perform
();
205
void
cancel
();
206
void
extension
();
207
208
private
:
209
int
steps;
210
QProgressDialog *pd;
211
QTimer *t;
212
};
213
214
//! [4]
215
// Operation constructor
216
Operation
::
Operation
(QObject *parent)
217
:
QObject
(
parent
), steps(0)
218
{
219
pd =
new
QProgressDialog(
"Operation in progress."
,
"Cancel"
, 0, 100);
220
connect(pd, &QProgressDialog::canceled,
this
, &
Operation
::
cancel
);
221
t =
new
QTimer(
this
);
222
connect(t, &QTimer::timeout,
this
, &
Operation
::
perform
);
223
t->start(0);
224
}
225
//! [4] //! [5]
226
227
void
Operation
::
perform
()
228
{
229
pd->setValue(steps);
230
//... perform one percent of the operation
231
steps++;
232
if
(steps > pd->maximum())
233
t->stop();
234
}
235
//! [5] //! [6]
236
237
void
Operation
::
cancel
()
238
{
239
t->stop();
240
//... cleanup
241
}
242
//! [6]
243
244
void
Operation
::
extension
()
245
{
246
using
ExtendedControls = QWidget;
247
QPushButton *findButton;
248
QPushButton *moreButton;
249
QWidget *extension;
250
QVBoxLayout *mainLayout;
251
252
//! [extension]
253
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
254
255
findButton =
new
QPushButton(tr(
"&Find"
));
256
moreButton =
new
QPushButton(tr(
"&More..."
));
257
moreButton->setCheckable(
true
);
258
259
extension =
new
ExtendedControls;
260
mainLayout->addWidget(extension);
261
extension->hide();
262
263
connect(moreButton, &QAbstractButton::toggled, extension, &QWidget::setVisible);
264
//! [extension]
265
266
//! [buttonbox]
267
QDialogButtonBox *buttonBox =
new
QDialogButtonBox(Qt::Vertical);
268
buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
269
buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);
270
//! [buttonbox]
271
}
272
273
int
main
()
274
{
275
}
EditorWindow
Definition
dialogs.cpp:28
EditorWindow::find
void find()
[0]
Definition
dialogs.cpp:39
EditorWindow::findNext
void findNext()
EditorWindow::countWords
void countWords()
[0]
Definition
dialogs.cpp:54
FindDialog
Definition
dialogs.cpp:17
Operation
Definition
dialogs.cpp:201
Operation::cancel
void cancel()
[5] //! [6]
Definition
dialogs.cpp:237
Operation::extension
void extension()
[6]
Definition
dialogs.cpp:244
Operation::Operation
Operation(QObject *parent)
[4]
Definition
dialogs.cpp:216
Operation::perform
void perform()
[4] //! [5]
Definition
dialogs.cpp:227
setWordCount
#define setWordCount(x)
Definition
dialogs.cpp:9
moo
void moo()
[1]
Definition
dialogs.cpp:182
WordCountDialog
QDialog WordCountDialog
Definition
dialogs.cpp:6
main
int main()
[open]
Definition
doc_src_objecttrees.cpp:7
tr
QString tr(const char *)
Definition
src_corelib_global_qglobal_widgets.cpp:67
qtbase
src
widgets
doc
snippets
dialogs
dialogs.cpp
Generated on
for Qt by
1.18.0