From 4d15a2fa4833201a8c33c995f05113ed64f5c795 Mon Sep 17 00:00:00 2001 From: wh201906 Date: Sun, 21 May 2023 13:48:48 +0800 Subject: [PATCH] Change theme without restarting app For issue #29 --- src/main.cpp | 25 ------------------------- src/mainwindow.cpp | 27 +++++++++++++++++++++++++++ src/mainwindow.h | 3 +++ src/settingstab.cpp | 5 ++++- src/settingstab.h | 1 + src/ui/settingstab.ui | 2 +- 6 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 824f704..df80108 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -97,31 +97,6 @@ int main(int argc, char *argv[]) QString theme = m_settings->value("Theme_Name").toString(); m_settings->endGroup(); - QFile* themeFile = new QFile(); - QTextStream* themeStream = new QTextStream(); - QString qssString = a.styleSheet(); // default behavior - if(theme == "(none)") - ; - else if(theme == "qdss_dark") - { - themeFile->setFileName(":/qdarkstyle/dark/darkstyle.qss"); - themeFile->open(QFile::ReadOnly | QFile::Text); - themeStream->setDevice(themeFile); - qssString = themeStream->readAll(); - } - else if(theme == "qdss_light") - { - themeFile->setFileName(":/qdarkstyle/light/lightstyle.qss"); - themeFile->open(QFile::ReadOnly | QFile::Text); - themeStream->setDevice(themeFile); - qssString = themeStream->readAll(); - } - a.setStyleSheet(qssString); - delete themeFile; - delete themeStream; - themeFile = nullptr; - themeStream = nullptr; - m_settings = nullptr; MainWindow w; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 7c6b0f2..26b15bb 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -14,6 +14,8 @@ MainWindow::MainWindow(QWidget *parent) , ui(new Ui::MainWindow) { ui->setupUi(this); + // might not be empty(specified by -stylesheet option) + m_appDefaultQss = qApp->styleSheet(); contextMenu = new QMenu(); IOConnection = new Connection(); @@ -77,6 +79,7 @@ MainWindow::MainWindow(QWidget *parent) ui->funcTab->insertTab(4, fileTab, tr("File")); settingsTab = new SettingsTab(); + connect(settingsTab, &SettingsTab::themeChanged, this, &MainWindow::onThemeChanged); connect(settingsTab, &SettingsTab::opacityChanged, this, &MainWindow::onOpacityChanged); // not a slot function, but works fine. connect(settingsTab, &SettingsTab::fullScreenStateChanged, this, &MainWindow::setFullScreen); connect(settingsTab, &SettingsTab::updateAvailableDeviceTypes, deviceTab, &DeviceTab::getAvailableTypes); @@ -532,6 +535,30 @@ void MainWindow::onOpacityChanged(qreal value) } } +void MainWindow::onThemeChanged(const QString& themeName) +{ + QFile themeFile; + QTextStream themeStream; + QString qssString = qApp->styleSheet(); // default behavior + if(themeName == "(none)") + qssString = m_appDefaultQss; + else if(themeName == "qdss_dark") + { + themeFile.setFileName(":/qdarkstyle/dark/darkstyle.qss"); + themeFile.open(QFile::ReadOnly | QFile::Text); + themeStream.setDevice(&themeFile); + qssString = themeStream.readAll(); + } + else if(themeName == "qdss_light") + { + themeFile.setFileName(":/qdarkstyle/light/lightstyle.qss"); + themeFile.open(QFile::ReadOnly | QFile::Text); + themeStream.setDevice(&themeFile); + qssString = themeStream.readAll(); + } + qApp->setStyleSheet(qssString); +} + void MainWindow::dockInit() { setDockNestingEnabled(true); diff --git a/src/mainwindow.h b/src/mainwindow.h index 1c0a0d0..1480084 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -56,6 +56,7 @@ public slots: void showUpTab(int tabID); void setFullScreen(bool isFullScreen); void onOpacityChanged(qreal value); + void onThemeChanged(const QString& themeName); void onDockTopLevelChanged(bool topLevel); // for opacity protected: @@ -113,6 +114,8 @@ private slots: SettingsTab* settingsTab; QList dockList; + QString m_appDefaultQss; + #ifndef Q_OS_ANDROID QCheckBox* onTopBox; diff --git a/src/settingstab.cpp b/src/settingstab.cpp index ecbf3cb..8e7fadb 100644 --- a/src/settingstab.cpp +++ b/src/settingstab.cpp @@ -231,6 +231,7 @@ void SettingsTab::loadPreference() on_Android_forceLandscapeBox_clicked(); // Android_dockBox only affect the config file #else + on_Theme_setButton_clicked(); on_Opacity_Box_valueChanged(ui->Opacity_Box->value()); #endif if(fontValid) @@ -338,8 +339,10 @@ void SettingsTab::on_Conf_exportButton_clicked() void SettingsTab::on_Theme_setButton_clicked() { + QString themeName = ui->Theme_nameBox->currentData().toString(); m_settings->beginGroup("SerialTest"); - m_settings->setValue("Theme_Name", ui->Theme_nameBox->currentData().toString()); + m_settings->setValue("Theme_Name", themeName); m_settings->endGroup(); + emit themeChanged(themeName); } diff --git a/src/settingstab.h b/src/settingstab.h index f002e6c..1e161e7 100644 --- a/src/settingstab.h +++ b/src/settingstab.h @@ -59,6 +59,7 @@ private slots: MySettings* m_settings; void createConfFile(const QString &path, bool overwrite = false); signals: + void themeChanged(const QString& themeName); void opacityChanged(qreal value); void fontChanged(QFont font); void fullScreenStateChanged(bool isFullScreen); diff --git a/src/ui/settingstab.ui b/src/ui/settingstab.ui index 2d27d40..b0898e5 100644 --- a/src/ui/settingstab.ui +++ b/src/ui/settingstab.ui @@ -322,7 +322,7 @@ If no config file is detected, This app will create one in the current working d - Theme *: + Theme: