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_kernel_qguiapplication.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#include <QApplication>
4#include <QMessageBox>
5#include <QSessionManager>
6#include <QWidget>
7
9struct MyMainWidget : public QWidget
10{
11 MyMainWidget(QWidget *parent);
12 void commitData(QSessionManager& manager);
13 bool saveDocument() { return true; };
14 QStringList restartCommand() { return QStringList(); };
15 QStringList discardCommand() { return QStringList(); };
16};
18void do_something(QString command) { Q_UNUSED(command); };
20
21//! [0]
22int main(int argc, char *argv[])
23{
24 QApplication::setDesktopSettingsAware(false);
25 QApplication app(argc, argv);
26 // ...
27 return app.exec();
28}
29//! [0]
30
31
32//! [1]
33MyMainWidget::MyMainWidget(QWidget *parent)
35{
36 connect(qApp, &QGuiApplication::commitDataRequest,
37 this, &MyMainWidget::commitData);
38}
39
40void MyMainWidget::commitData(QSessionManager& manager)
41{
42 if (manager.allowsInteraction()) {
43 int ret = QMessageBox::warning(
44 mainWindow,
45 tr("My Application"),
46 tr("Save changes to document?"),
47 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
48
49 switch (ret) {
50 case QMessageBox::Save:
51 manager.release();
52 if (!saveDocument())
53 manager.cancel();
54 break;
55 case QMessageBox::Discard:
56 break;
57 case QMessageBox::Cancel:
58 default:
59 manager.cancel();
60 }
61 } else {
62 // we did not get permission to interact, then
63 // do something reasonable instead
64 }
65}
66//! [1]
67
68
69/* wrap snippet 2
70
71//! [2]
72appname -session id
73//! [2]
74
75*/ // wrap snippet 2
76
77} // src_gui_kernel_qguiapplication
MyMainWidget mySession(nullptr)