From 47c7344fb251d0147168f389b572024e502c58ee Mon Sep 17 00:00:00 2001 From: gongheng Date: Thu, 20 Nov 2025 20:34:42 +0800 Subject: [PATCH] Fix: Add the functions of saving and restoring window status -- Realize the saving and restoration of the window state, including maximizing the state and size. The relevant functions have been updated to ensure that the state is correctly saved when the window is maximized and the saved state is applied when restored. Log: Add the functions of saving and restoring window status Bug: https://pms.uniontech.com/bug-view-318459.html --- src/source/common/uistruct.h | 1 + src/source/mainwindow.cpp | 61 +++++++++++++++++---- src/source/mainwindow.h | 17 +++--- tests/UnitTest/src/source/ut_mainwindow.cpp | 7 --- 4 files changed, 61 insertions(+), 25 deletions(-) diff --git a/src/source/common/uistruct.h b/src/source/common/uistruct.h index 8487b3396..c2ab051ca 100644 --- a/src/source/common/uistruct.h +++ b/src/source/common/uistruct.h @@ -15,6 +15,7 @@ #define TEMPPATH DStandardPaths::writableLocation(QStandardPaths::TempLocation) // 临时路径(打开等操作) #define MAINWINDOW_WIDTH_NAME "MainWindowWidthName" // 主界宽 #define MAINWINDOW_HEIGHT_NAME "MainWindowHeightName" // 主界面高 +#define MAINWINDOW_STATE_NAME "MainWindowStateName" // 主界面窗口状态 #define ORDER_JSON "OrderJson" // 编辑json #define ORDER_EDIT "edit" // 编辑权限 #define ORDER_RENAME "rename" // 重命名权限 diff --git a/src/source/mainwindow.cpp b/src/source/mainwindow.cpp index dbb907bfa..d5c901346 100644 --- a/src/source/mainwindow.cpp +++ b/src/source/mainwindow.cpp @@ -40,6 +40,10 @@ #include #include #include +#ifdef DTKCORE_CLASS_DConfigFile +#include +DCORE_USE_NAMESPACE +#endif #include #include @@ -51,10 +55,6 @@ #include #include #include -#ifdef DTKCORE_CLASS_DConfigFile -#include -DCORE_USE_NAMESPACE -#endif static QMutex mutex; // 静态全局变量只在定义该变量的源文件内有效 #define FILE_TRUNCATION_LENGTH 70 @@ -114,7 +114,7 @@ MainWindow::~MainWindow() qDebug() << "MainWindow destructor started"; // 保存窗口大小状态 qDebug() << "Saving window size configuration"; - saveConfigWinSize(width(), height()); + saveConfigWinState(); qDebug() << "Destroying ArchiveManager instance"; ArchiveManager::get_instance()->destory_instance(); @@ -241,7 +241,7 @@ void MainWindow::initData() } qDebug() << "Setting window size and minimum size"; - resize(getConfigWinSize()); // 设置窗口尺寸 + restoreConfigWinState(); // 恢复窗口状态(大小和最大化状态) setMinimumSize(620, 300); // task 16309调整最小大小 qDebug() << "Application data initialized"; } @@ -2459,15 +2459,54 @@ QSize MainWindow::getConfigWinSize() return QSize(winWidth, winHeight); } -void MainWindow::saveConfigWinSize(int w, int h) +void MainWindow::saveConfigWinState() { - int winWidth = w > MAINWINDOW_DEFAULTW ? w : MAINWINDOW_DEFAULTW; - int winHeight = h > MAINWINDOW_DEFAULTH ? h : MAINWINDOW_DEFAULTH; - m_pSettings->setValue(MAINWINDOW_HEIGHT_NAME, winHeight); - m_pSettings->setValue(MAINWINDOW_WIDTH_NAME, winWidth); + // 如果窗口是最大化状态,保存最大化标志和正常尺寸 + if (isMaximized()) { + m_pSettings->setValue(MAINWINDOW_STATE_NAME, Qt::WindowMaximized); + // 对于最大化窗口,我们需要保存正常尺寸而不是最大化尺寸 + // 使用默认尺寸或之前保存的正常尺寸 + QVariant tempWidth = m_pSettings->value(MAINWINDOW_WIDTH_NAME); + QVariant tempHeight = m_pSettings->value(MAINWINDOW_HEIGHT_NAME); + + if (!tempWidth.isValid() || !tempHeight.isValid()) { + // 如果没有之前保存的尺寸,使用默认尺寸 + m_pSettings->setValue(MAINWINDOW_WIDTH_NAME, MAINWINDOW_DEFAULTW); + m_pSettings->setValue(MAINWINDOW_HEIGHT_NAME, MAINWINDOW_DEFAULTH); + } + // 如果有之前保存的尺寸,保持不变(不更新为最大化尺寸) + } else { + // 正常状态,保存当前尺寸和状态 + m_pSettings->setValue(MAINWINDOW_STATE_NAME, Qt::WindowNoState); + // 保存窗口尺寸 + int w = width(); + int h = height(); + int winWidth = w > MAINWINDOW_DEFAULTW ? w : MAINWINDOW_DEFAULTW; + int winHeight = h > MAINWINDOW_DEFAULTH ? h : MAINWINDOW_DEFAULTH; + m_pSettings->setValue(MAINWINDOW_HEIGHT_NAME, winHeight); + m_pSettings->setValue(MAINWINDOW_WIDTH_NAME, winWidth); + } m_pSettings->sync(); } +void MainWindow::restoreConfigWinState() +{ + // 获取保存的窗口状态 + Qt::WindowState savedState = static_cast( + m_pSettings->value(MAINWINDOW_STATE_NAME, Qt::WindowNoState).toInt()); + + // 先设置正常尺寸 + resize(getConfigWinSize()); + + // 然后应用状态 + if (savedState == Qt::WindowMaximized) { + // 使用QTimer延迟执行,确保窗口完全初始化后再最大化 + QTimer::singleShot(0, this, [this]() { + showMaximized(); + }); + } +} + void MainWindow::convertArchive(const QString &convertType) { qInfo() << "对压缩包进行格式转换" << convertType; diff --git a/src/source/mainwindow.h b/src/source/mainwindow.h index 6f9acabf6..839050bbf 100644 --- a/src/source/mainwindow.h +++ b/src/source/mainwindow.h @@ -85,6 +85,16 @@ public : */ void handleQuit(); + /** + * @brief saveConfigWinState 保存窗口状态(大小和最大化状态) + */ + void saveConfigWinState(); + + /** + * @brief restoreConfigWinState 恢复窗口状态(大小和最大化状态) + */ + void restoreConfigWinState(); + private: /** * @brief initUI 初始化界面 @@ -224,13 +234,6 @@ public : */ QSize getConfigWinSize(); - /** - * @brief saveConfigWinSize 保存窗口尺寸 - * @param w 宽度 - * @param h 高度 - */ - void saveConfigWinSize(int w, int h); - /** * @brief convertArchive 格式转换 * @param convertType 转换后的文件类型 diff --git a/tests/UnitTest/src/source/ut_mainwindow.cpp b/tests/UnitTest/src/source/ut_mainwindow.cpp index e0ba2ed3b..2656513e8 100644 --- a/tests/UnitTest/src/source/ut_mainwindow.cpp +++ b/tests/UnitTest/src/source/ut_mainwindow.cpp @@ -1555,13 +1555,6 @@ TEST_F(UT_MainWindow, test_getConfigWinSize) EXPECT_NE(m_tester->getConfigWinSize().width(), 0); } -TEST_F(UT_MainWindow, testsaveConfigWinSize) -{ - m_tester->saveConfigWinSize(800, 600); - EXPECT_EQ(m_tester->m_pSettings->value(MAINWINDOW_WIDTH_NAME).toInt(), 800); - EXPECT_EQ(m_tester->m_pSettings->value(MAINWINDOW_HEIGHT_NAME).toInt(), 600); -} - TEST_F(UT_MainWindow, test_convertArchive) { Stub stub;