Skip to content

Commit 0c94529

Browse files
pppanghu77deepin-bot[bot]
authored andcommitted
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
1 parent 195db0d commit 0c94529

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

src/source/common/uistruct.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define TEMPPATH DStandardPaths::writableLocation(QStandardPaths::TempLocation) // 临时路径(打开等操作)
1616
#define MAINWINDOW_WIDTH_NAME "MainWindowWidthName" // 主界宽
1717
#define MAINWINDOW_HEIGHT_NAME "MainWindowHeightName" // 主界面高
18+
#define MAINWINDOW_STATE_NAME "MainWindowStateName" // 主界面窗口状态
1819
#define ORDER_JSON "OrderJson" // 编辑json
1920
#define ORDER_EDIT "edit" // 编辑权限
2021
#define ORDER_RENAME "rename" // 重命名权限

src/source/mainwindow.cpp

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <QFormLayout>
5050
#include <QShortcut>
5151
#include <QJsonObject>
52+
#include <QTimer>
5253
#ifdef DTKCORE_CLASS_DConfigFile
5354
#include <DConfig>
5455
DCORE_USE_NAMESPACE
@@ -90,7 +91,7 @@ MainWindow::MainWindow(QWidget *parent)
9091
MainWindow::~MainWindow()
9192
{
9293
// 保存窗口大小状态
93-
saveConfigWinSize(width(), height());
94+
saveConfigWinState();
9495

9596
ArchiveManager::get_instance()->destory_instance();
9697

@@ -197,7 +198,7 @@ void MainWindow::initData()
197198
m_pSettings->setValue("dir", "");
198199
}
199200

200-
resize(getConfigWinSize()); // 设置窗口尺寸
201+
restoreConfigWinState(); // 恢复窗口状态(大小和最大化状态)
201202
setMinimumSize(620, 300); // task 16309调整最小大小
202203
}
203204

@@ -2243,15 +2244,54 @@ QSize MainWindow::getConfigWinSize()
22432244
return QSize(winWidth, winHeight);
22442245
}
22452246

2246-
void MainWindow::saveConfigWinSize(int w, int h)
2247+
void MainWindow::saveConfigWinState()
22472248
{
2248-
int winWidth = w > MAINWINDOW_DEFAULTW ? w : MAINWINDOW_DEFAULTW;
2249-
int winHeight = h > MAINWINDOW_DEFAULTH ? h : MAINWINDOW_DEFAULTH;
2250-
m_pSettings->setValue(MAINWINDOW_HEIGHT_NAME, winHeight);
2251-
m_pSettings->setValue(MAINWINDOW_WIDTH_NAME, winWidth);
2249+
// 如果窗口是最大化状态,保存最大化标志和正常尺寸
2250+
if (isMaximized()) {
2251+
m_pSettings->setValue(MAINWINDOW_STATE_NAME, Qt::WindowMaximized);
2252+
// 对于最大化窗口,我们需要保存正常尺寸而不是最大化尺寸
2253+
// 使用默认尺寸或之前保存的正常尺寸
2254+
QVariant tempWidth = m_pSettings->value(MAINWINDOW_WIDTH_NAME);
2255+
QVariant tempHeight = m_pSettings->value(MAINWINDOW_HEIGHT_NAME);
2256+
2257+
if (!tempWidth.isValid() || !tempHeight.isValid()) {
2258+
// 如果没有之前保存的尺寸,使用默认尺寸
2259+
m_pSettings->setValue(MAINWINDOW_WIDTH_NAME, MAINWINDOW_DEFAULTW);
2260+
m_pSettings->setValue(MAINWINDOW_HEIGHT_NAME, MAINWINDOW_DEFAULTH);
2261+
}
2262+
// 如果有之前保存的尺寸,保持不变(不更新为最大化尺寸)
2263+
} else {
2264+
// 正常状态,保存当前尺寸和状态
2265+
m_pSettings->setValue(MAINWINDOW_STATE_NAME, Qt::WindowNoState);
2266+
// 保存窗口尺寸
2267+
int w = width();
2268+
int h = height();
2269+
int winWidth = w > MAINWINDOW_DEFAULTW ? w : MAINWINDOW_DEFAULTW;
2270+
int winHeight = h > MAINWINDOW_DEFAULTH ? h : MAINWINDOW_DEFAULTH;
2271+
m_pSettings->setValue(MAINWINDOW_HEIGHT_NAME, winHeight);
2272+
m_pSettings->setValue(MAINWINDOW_WIDTH_NAME, winWidth);
2273+
}
22522274
m_pSettings->sync();
22532275
}
22542276

2277+
void MainWindow::restoreConfigWinState()
2278+
{
2279+
// 获取保存的窗口状态
2280+
Qt::WindowState savedState = static_cast<Qt::WindowState>(
2281+
m_pSettings->value(MAINWINDOW_STATE_NAME, Qt::WindowNoState).toInt());
2282+
2283+
// 先设置正常尺寸
2284+
resize(getConfigWinSize());
2285+
2286+
// 然后应用状态
2287+
if (savedState == Qt::WindowMaximized) {
2288+
// 使用QTimer延迟执行,确保窗口完全初始化后再最大化
2289+
QTimer::singleShot(0, this, [this]() {
2290+
showMaximized();
2291+
});
2292+
}
2293+
}
2294+
22552295
void MainWindow::convertArchive(const QString &convertType)
22562296
{
22572297
qInfo() << "对压缩包进行格式转换" << convertType;

src/source/mainwindow.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ public :
8484
*/
8585
void handleQuit();
8686

87+
/**
88+
* @brief saveConfigWinState 保存窗口状态(大小和最大化状态)
89+
*/
90+
void saveConfigWinState();
91+
92+
/**
93+
* @brief restoreConfigWinState 恢复窗口状态(大小和最大化状态)
94+
*/
95+
void restoreConfigWinState();
96+
8797
private:
8898
/**
8999
* @brief initUI 初始化界面
@@ -223,13 +233,6 @@ public :
223233
*/
224234
QSize getConfigWinSize();
225235

226-
/**
227-
* @brief saveConfigWinSize 保存窗口尺寸
228-
* @param w 宽度
229-
* @param h 高度
230-
*/
231-
void saveConfigWinSize(int w, int h);
232-
233236
/**
234237
* @brief convertArchive 格式转换
235238
* @param convertType 转换后的文件类型

tests/UnitTest/src/source/ut_mainwindow.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,12 +1555,6 @@ TEST_F(UT_MainWindow, test_getConfigWinSize)
15551555
EXPECT_NE(m_tester->getConfigWinSize().width(), 0);
15561556
}
15571557

1558-
TEST_F(UT_MainWindow, testsaveConfigWinSize)
1559-
{
1560-
m_tester->saveConfigWinSize(800, 600);
1561-
EXPECT_EQ(m_tester->m_pSettings->value(MAINWINDOW_WIDTH_NAME).toInt(), 800);
1562-
EXPECT_EQ(m_tester->m_pSettings->value(MAINWINDOW_HEIGHT_NAME).toInt(), 600);
1563-
}
15641558

15651559
TEST_F(UT_MainWindow, test_convertArchive)
15661560
{

0 commit comments

Comments
 (0)