Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
8
9#define this 0
10#define setWordCount(x) isVisible()
11
12QString tr(const char *text)
13{
15}
16
18{
19public:
20 void find();
21 void countWords();
22
23private:
24 FindDialog *findDialog;
25};
26
29{
30 if (!findDialog) {
31 findDialog = new FindDialog(this);
32 connect(findDialog, &FindDialog::findNext,
33 this, &EditorWindow::findNext);
34 }
35
36 findDialog->show();
37 findDialog->raise();
38 findDialog->activateWindow();
39}
41
44{
46 dialog.setWordCount(document().wordCount());
47 dialog.exec();
48}
50
51inline bool boo()
52{
53 QMessageBox::information(this, "Application name",
54 "Unable to find the user preferences file.\n"
55 "The factory default will be used instead.");
56
57 QString filename;
58 if (QFile::exists(filename) &&
60 this,
61 tr("Overwrite File? -- Application Name"),
62 tr("A file called %1 already exists."
63 "Do you want to overwrite it?")
64 .arg(filename),
65 tr("&Yes"), tr("&No"),
66 QString(), 0, 1))
67 return false;
68
69 switch(QMessageBox::warning(this, "Application name",
70 "Could not connect to the <mumble> server.\n"
71 "This program can't function correctly "
72 "without the server.\n\n",
73 "Retry",
74 "Quit", 0, 0, 1)) {
75 case 0: // The user clicked the Retry again button or pressed Enter
76 // try again
77 break;
78 case 1: // The user clicked the Quit or pressed Escape
79 // exit
80 break;
81 }
82
83 switch(QMessageBox::information(this, "Application name here",
84 "The document contains unsaved changes\n"
85 "Do you want to save the changes before exiting?",
86 "&Save", "&Discard", "Cancel",
87 0, // Enter == button 0
88 2)) { // Escape == button 2
89 case 0: // Save clicked or Alt+S pressed or Enter pressed.
90 // save
91 break;
92 case 1: // Discard clicked or Alt+D pressed
93 // don't save but exit
94 break;
95 case 2: // Cancel clicked or Escape pressed
96 // don't exit
97 break;
98 }
99
100 switch(QMessageBox::warning(this, "Application name here",
101 "Could not save the user preferences,\n"
102 "because the disk is full. You can delete\n"
103 "some files and press Retry, or you can\n"
104 "abort the Save Preferences operation.",
107 case QMessageBox::Retry: // Retry clicked or Enter pressed
108 // try again
109 break;
110 case QMessageBox::Abort: // Abort clicked or Escape pressed
111 // abort
112 break;
113 }
114
115 QString errorDetails;
116 QMessageBox::critical(0, "Application name here",
117 QString("An internal error occurred. Please ") +
118 "call technical support at 1234-56789 and report\n"+
119 "these numbers:\n\n" + errorDetails +
120 "\n\nApplication will now exit.");
121
122 QMessageBox::about(this, "About <Application>",
123 "<Application> is a <one-paragraph blurb>\n\n"
124 "Copyright 1991-2003 Such-and-such. "
125 "<License words here.>\n\n"
126 "For technical support, call 1234-56789 or see\n"
127 "http://www.such-and-such.com/Application/\n");
128
129 {
130 // saving the file
131 QMessageBox mb("Application name here",
132 "Saving the file will overwrite the original file on the disk.\n"
133 "Do you really want to save?",
138 mb.setButtonText(QMessageBox::Yes, "Save");
139 mb.setButtonText(QMessageBox::No, "Discard");
140 switch(mb.exec()) {
141 case QMessageBox::Yes:
142 // save and exit
143 break;
144 case QMessageBox::No:
145 // exit without saving
146 break;
148 // don't save and don't exit
149 break;
150 }
151 }
152
153 {
154 // hardware failure
156 QMessageBox mb("Application Name",
157 "Hardware failure.\n\nDisk error detected\nDo you want to stop?",
162 if (mb.exec() == QMessageBox::No) {
163 // try again
165 }
166 }
167}
168
169inline void moo()
170{
171 int numFiles;
173 QProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this);
175
176 for (int i = 0; i < numFiles; i++) {
177 progress.setValue(i);
178
179 if (progress.wasCanceled())
180 break;
181 //... copy one file
182 }
183 progress.setValue(numFiles);
185}
186
187class Operation : public QObject
188{
189public:
191 void perform();
192 void cancel();
193
194private:
195 int steps;
196 QProgressDialog *pd;
197 QTimer *t;
198};
199
201// Operation constructor
203 : QObject(parent), steps(0)
204{
205 pd = new QProgressDialog("Operation in progress.", "Cancel", 0, 100);
207 t = new QTimer(this);
209 t->start(0);
210}
212
214{
215 pd->setValue(steps);
216 //... perform one percent of the operation
217 steps++;
218 if (steps > pd->maximum())
219 t->stop();
220}
222
224{
225 t->stop();
226 //... cleanup
227}
229
231{
232 using ExtendedControls = QWidget;
233 QPushButton *findButton;
234 QPushButton *moreButton;
236 QVBoxLayout *mainLayout;
237
240
241 findButton = new QPushButton(tr("&Find"));
242 moreButton = new QPushButton(tr("&More..."));
243 moreButton->setCheckable(true);
244
245 extension = new ExtendedControls;
246 mainLayout->addWidget(extension);
247 extension->hide();
248
251
254 buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
255 buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);
257}
258
259int main()
260{
261}
void find()
[0]
Definition dialogs.cpp:28
void countWords()
[0]
Definition dialogs.cpp:43
void cancel()
[5] //! [6]
Definition dialogs.cpp:223
Operation(QObject *parent)
[4]
Definition dialogs.cpp:202
void perform()
[4] //! [5]
Definition dialogs.cpp:213
void toggled(bool checked)
This signal is emitted whenever a checkable button changes its state.
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=Qt::Alignment())
Adds widget to the end of this box layout, with a stretch factor of stretch and alignment alignment.
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
The QDialogButtonBox class is a widget that presents buttons in a layout that is appropriate to the c...
void addButton(QAbstractButton *button, ButtonRole role)
Adds the given button to the button box with the specified role.
The QDialog class is the base class of dialog windows.
Definition qdialog.h:19
virtual int exec()
Shows the dialog as a \l{QDialog::Modal Dialogs}{modal dialog}, blocking until the user closes it.
Definition qdialog.cpp:543
bool exists() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qfile.cpp:351
void setSizeConstraint(SizeConstraint)
Definition qlayout.cpp:1241
@ SetFixedSize
Definition qlayout.h:39
The QMainWindow class provides a main application window.
Definition qmainwindow.h:25
The QMessageBox class provides a modal dialog for informing the user or for asking the user a questio...
Definition qmessagebox.h:22
static StandardButton warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
static StandardButton information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
static StandardButton critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
static void about(QWidget *parent, const QString &title, const QString &text)
Displays a simple about box with title title and text text.
static StandardButton question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=StandardButtons(Yes|No), StandardButton defaultButton=NoButton)
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
The QProgressDialog class provides feedback on the progress of a slow operation.
void canceled()
This signal is emitted when the cancel button is clicked.
void setValue(int progress)
bool wasCanceled
whether the dialog was canceled
int maximum
the highest value represented by the progress bar
The QPushButton widget provides a command button.
Definition qpushbutton.h:20
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qtimer.h:20
void timeout(QPrivateSignal)
This signal is emitted when the timer times out.
The QVBoxLayout class lines up widgets vertically.
Definition qboxlayout.h:91
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void raise()
Raises this widget to the top of the parent widget's stack.
void setWindowModality(Qt::WindowModality windowModality)
Definition qwidget.cpp:2799
void show()
Shows the widget and its child widgets.
Definition qwidget.cpp:7889
virtual void setVisible(bool visible)
Definition qwidget.cpp:8269
void activateWindow()
Sets the top-level widget containing this widget to be the active window.
QPushButton
[1]
void extension()
[6]
Definition dialogs.cpp:230
void moo()
Definition dialogs.cpp:169
bool boo()
[1]
Definition dialogs.cpp:51
QDialog FindDialog
Definition dialogs.cpp:7
QDialog WordCountDialog
Definition dialogs.cpp:6
int main()
Definition dialogs.cpp:259
QString text
@ WindowModal
@ Vertical
Definition qnamespace.h:100
GLdouble GLdouble t
Definition qopenglext.h:243
SSL_CTX int void * arg
#define tr(X)
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QFileDialog dialog(this)
[1]