Use aboutToQuit() signal for cleanup-code
As recommended by the Qt documentation
This commit is contained in:
parent
b6acb55176
commit
7ac57bb5a1
|
@ -10,23 +10,32 @@
|
|||
#include <qtutilities/resources/resources.h>
|
||||
#include <qtutilities/settingsdialog/qtsettings.h>
|
||||
|
||||
#include <passwordfile/util/openssl.h>
|
||||
|
||||
#include <QApplication>
|
||||
#include <QFile>
|
||||
#include <QSettings>
|
||||
|
||||
using namespace CppUtilities;
|
||||
using namespace QtUtilities;
|
||||
using namespace Util;
|
||||
|
||||
namespace QtGui {
|
||||
|
||||
int runWidgetsGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs, const QString &file)
|
||||
{
|
||||
SET_QT_APPLICATION_INFO;
|
||||
|
||||
// init OpenSSL
|
||||
OpenSsl::init();
|
||||
|
||||
// init application
|
||||
QApplication a(argc, argv);
|
||||
QApplication application(argc, argv);
|
||||
|
||||
// restore Qt settings
|
||||
QtSettings qtSettings;
|
||||
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QStringLiteral(PROJECT_NAME));
|
||||
|
||||
// move old config to new location
|
||||
const QString oldConfig
|
||||
= QSettings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(), QApplication::applicationName()).fileName();
|
||||
|
@ -34,19 +43,20 @@ int runWidgetsGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs,
|
|||
settings.sync();
|
||||
qtSettings.restore(settings);
|
||||
qtSettings.apply();
|
||||
|
||||
// apply settings specified via command line args
|
||||
qtConfigArgs.applySettings(qtSettings.hasCustomFont());
|
||||
LOAD_QT_TRANSLATIONS;
|
||||
|
||||
// init widgets GUI
|
||||
MainWindow w(settings, &qtSettings);
|
||||
w.show();
|
||||
if (!file.isEmpty()) {
|
||||
w.openFile(file);
|
||||
}
|
||||
|
||||
// start event loop
|
||||
int res = a.exec();
|
||||
// save Qt settings
|
||||
qtSettings.save(settings);
|
||||
return res;
|
||||
QObject::connect(&application, &QCoreApplication::aboutToQuit, &OpenSsl::clean);
|
||||
return application.exec();
|
||||
}
|
||||
} // namespace QtGui
|
||||
|
|
|
@ -286,10 +286,12 @@ void MainWindow::closeEvent(QCloseEvent *event)
|
|||
event->ignore();
|
||||
return;
|
||||
}
|
||||
|
||||
// close undow view
|
||||
if (m_undoView) {
|
||||
m_undoView->close();
|
||||
}
|
||||
|
||||
// save settings
|
||||
m_settings.beginGroup(QStringLiteral("mainwindow"));
|
||||
m_settings.setValue(QStringLiteral("geometry"), saveGeometry());
|
||||
|
@ -307,6 +309,9 @@ void MainWindow::closeEvent(QCloseEvent *event)
|
|||
}
|
||||
m_settings.setValue(QStringLiteral("pwvisibility"), pwVisibility);
|
||||
m_settings.endGroup();
|
||||
if (m_qtSettings) {
|
||||
m_qtSettings->save(m_settings);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::timerEvent(QTimerEvent *event)
|
||||
|
@ -328,7 +333,6 @@ void MainWindow::showSettingsDialog()
|
|||
m_settingsDlg->setWindowTitle(tr("Qt settings"));
|
||||
m_settingsDlg->setSingleCategory(m_qtSettings->category());
|
||||
}
|
||||
//connect(m_settingsDlg, &SettingsDialog::applied, this, &MainWindow::settingsAccepted);
|
||||
}
|
||||
if (m_settingsDlg->isHidden()) {
|
||||
m_settingsDlg->showNormal();
|
||||
|
|
14
main.cpp
14
main.cpp
|
@ -16,6 +16,7 @@
|
|||
#include <c++utilities/misc/parseerror.h>
|
||||
|
||||
#if defined(PASSWORD_MANAGER_GUI_QTWIDGETS) || defined(PASSWORD_MANAGER_GUI_QTQUICK)
|
||||
#include <QCoreApplication>
|
||||
#include <QString>
|
||||
#include <qtutilities/resources/qtconfigarguments.h>
|
||||
ENABLE_QT_RESOURCES_OF_STATIC_DEPENDENCIES
|
||||
|
@ -68,18 +69,22 @@ int main(int argc, char *argv[])
|
|||
parser.parseArgs(argc, argv);
|
||||
#endif
|
||||
|
||||
// init OpenSSL
|
||||
OpenSsl::init();
|
||||
|
||||
#ifndef PASSWORD_MANAGER_FORCE_GUI
|
||||
// start either interactive CLI or GUI
|
||||
if (cliArg.isPresent()) {
|
||||
// init OpenSSL
|
||||
OpenSsl::init();
|
||||
|
||||
Cli::InteractiveCli cli;
|
||||
if (fileArg.isPresent()) {
|
||||
cli.run(fileArg.firstValue());
|
||||
} else {
|
||||
cli.run();
|
||||
}
|
||||
|
||||
// clean OpenSSL
|
||||
OpenSsl::clean();
|
||||
|
||||
} else if (qtConfigArgs.areQtGuiArgsPresent()) {
|
||||
#if defined(PASSWORD_MANAGER_GUI_QTWIDGETS) || defined(PASSWORD_MANAGER_GUI_QTQUICK)
|
||||
const auto file(fileArg.isPresent() ? QString::fromLocal8Bit(fileArg.firstValue()) : QString());
|
||||
|
@ -117,8 +122,5 @@ int main(int argc, char *argv[])
|
|||
#endif
|
||||
#endif
|
||||
|
||||
// clean OpenSSL
|
||||
OpenSsl::clean();
|
||||
|
||||
return returnCode;
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
#include <qtutilities/resources/qtconfigarguments.h>
|
||||
#include <qtutilities/resources/resources.h>
|
||||
|
||||
#include <passwordfile/util/openssl.h>
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QIcon>
|
||||
#include <QQmlApplicationEngine>
|
||||
|
@ -30,6 +32,7 @@
|
|||
#endif
|
||||
|
||||
using namespace CppUtilities;
|
||||
using namespace Util;
|
||||
|
||||
namespace QtGui {
|
||||
|
||||
|
@ -45,13 +48,16 @@ int runQuickGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs, c
|
|||
qputenv("XDG_CURRENT_DESKTOP", QByteArray("please don't override my settings"));
|
||||
}
|
||||
|
||||
// init OpenSSL
|
||||
OpenSsl::init();
|
||||
|
||||
// init application
|
||||
SET_QT_APPLICATION_INFO;
|
||||
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||
QApplication a(argc, argv);
|
||||
QApplication application(argc, argv);
|
||||
#else
|
||||
QGuiApplication a(argc, argv);
|
||||
QGuiApplication application(argc, argv);
|
||||
#endif
|
||||
|
||||
// apply settings specified via command line args
|
||||
|
@ -88,12 +94,13 @@ int runQuickGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs, c
|
|||
#endif
|
||||
auto *const context(engine.rootContext());
|
||||
context->setContextProperty(QStringLiteral("nativeInterface"), &controller);
|
||||
context->setContextProperty(QStringLiteral("app"), &a);
|
||||
context->setContextProperty(QStringLiteral("app"), &application);
|
||||
context->setContextProperty(QStringLiteral("description"), QStringLiteral(APP_DESCRIPTION));
|
||||
context->setContextProperty(QStringLiteral("dependencyVersions"), QStringList(DEPENCENCY_VERSIONS));
|
||||
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
|
||||
|
||||
// run event loop
|
||||
return a.exec();
|
||||
QObject::connect(&application, &QCoreApplication::aboutToQuit, &OpenSsl::clean);
|
||||
return application.exec();
|
||||
}
|
||||
} // namespace QtGui
|
||||
|
|
Loading…
Reference in New Issue