Repository navigation
Expand file tree
/
Copy pathtest_settings.cpp
More file actions
71 lines (58 loc) · 2.71 KB
/
Copy pathtest_settings.cpp
File metadata and controls
71 lines (58 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <QCoreApplication>
#include <QSettings>
#include <QDebug>
#include <QCheckBox>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 创建测试配置文件路径
QString configPath = QCoreApplication::applicationDirPath() + "/test_config.ini";
// 模拟程序启动时的设置加载
qDebug() << "=== 测试程序启动时加载设置 ===";
QSettings settings(configPath, QSettings::IniFormat);
// 模拟UI组件
QCheckBox checkBox_timestamp;
QCheckBox checkBox_logMode;
QCheckBox checkBox_hexReceive;
QCheckBox checkBox_hexSend;
// 应用默认设置
bool timestamp = settings.value("timestamp", true).toBool();
bool logMode = settings.value("logMode", true).toBool();
bool hexReceive = settings.value("hexReceive", false).toBool();
bool hexSend = settings.value("hexSend", false).toBool();
qDebug() << "从配置文件加载的设置:";
qDebug() << "timestamp: " << timestamp;
qDebug() << "logMode: " << logMode;
qDebug() << "hexReceive: " << hexReceive;
qDebug() << "hexSend: " << hexSend;
// 设置复选框状态
checkBox_timestamp.setChecked(timestamp);
checkBox_logMode.setChecked(logMode);
checkBox_hexReceive.setChecked(hexReceive);
checkBox_hexSend.setChecked(hexSend);
qDebug() << "设置复选框状态后:";
qDebug() << "timestamp: " << checkBox_timestamp.isChecked();
qDebug() << "logMode: " << checkBox_logMode.isChecked();
// 模拟用户修改设置
qDebug() << "\n=== 测试用户修改设置 ===";
checkBox_logMode.setChecked(false);
// 模拟保存设置
qDebug() << "\n=== 测试保存设置 ===";
settings.setValue("timestamp", checkBox_timestamp.isChecked());
settings.setValue("logMode", checkBox_logMode.isChecked());
settings.setValue("hexReceive", checkBox_hexReceive.isChecked());
settings.setValue("hexSend", checkBox_hexSend.isChecked());
settings.sync();
// 再次加载设置,验证保存是否成功
qDebug() << "\n=== 测试重新加载设置 ===";
QSettings settings2(configPath, QSettings::IniFormat);
qDebug() << "timestamp: " << settings2.value("timestamp", true).toBool();
qDebug() << "logMode: " << settings2.value("logMode", true).toBool();
// 测试默认值功能
qDebug() << "\n=== 测试默认值功能 ===";
QSettings settings3("non_existent.ini", QSettings::IniFormat);
qDebug() << "不存在的配置文件 - timestamp默认值: " << settings3.value("timestamp", true).toBool();
qDebug() << "不存在的配置文件 - logMode默认值: " << settings3.value("logMode", true).toBool();
qDebug() << "\n=== 测试完成 ===";
return 0;
}