QSetting读取.ini配置文件及设置对话框与主窗口的通信

QSetting的使用

整体流程为MainWindow构造函数内运行preference函数创建Setting类的实例,并运行实例的readSetting函数从本地config.ini文件读取值,然后运行实例的sendSignal函数将相应值以信号的方式发送给MainWindowMainWindow调用相应的槽函数进行处理。这样在主程序加载完成之时就可以保持设置和保存在本地文件的一致了。然后在MainWindow菜单栏点击Preference按钮的时候,根据checked的真假来决定Setting类的实例的显示。这样,配置文件的读取、设置对话框与主窗口的通信、主程序启动时自动读取设置三个问题就解决了。

主程序调用

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
Preference(false); // 主窗口构造函数中
void MainWindow::Preference(bool checked){
Setting *pre = new Setting(this);
connect(pre,SIGNAL(showMainMenuSignal(bool)),this,SLOT(showMainMenu(bool)));
connect(pre,SIGNAL(showToolBarSignal(bool)),this,SLOT(showToolBar(bool)));
connect(pre,SIGNAL(showStatusBarSignal(bool)),this,SLOT(showStatusBar(bool)));
connect(pre,SIGNAL(changeLang(int)),this,SLOT(changeLang(int)));
connect(pre,SIGNAL(setTabsClosableSignal(bool)),this,SLOT(setTabsClosable(bool)));
connect(pre,SIGNAL(setTabsMovableSignal(bool)),this,SLOT(setTabsMovable(bool)));
connect(pre,SIGNAL(setTabPositionSignal(int)),this,SLOT(setTabPosition(int)));
connect(pre,SIGNAL(setTabShapeSignal(int)),this,SLOT(setTabShape(int)));
connect(pre,SIGNAL(setTextColorSignal(QRgb)),this,SLOT(setTextColor(QRgb)));
connect(pre,SIGNAL(setTextbgcolorSignal(QRgb)),this,SLOT(setTextbgcolor(QRgb)));
connect(pre,SIGNAL(setbgcolorSignal(QRgb)),this,SLOT(setbgcolor(QRgb)));
connect(pre,SIGNAL(setcurRowColorSignal(QRgb)),this,SLOT(setcurRowColor(QRgb)));
connect(pre,SIGNAL(setThemeSignal(int)),this,SLOT(setWindowTheme(int)));
connect(pre,SIGNAL(newFileNameSignal(QString&)),this,SLOT(setNewFileName(QString&)));
connect(pre,SIGNAL(newFileSuffixSignal(QString&)),this,SLOT(setNewFileSuffix(QString&)));
pre->sendSignal();
// checked解决在启动主程序时就自动运行Preference读取设置且不显示对话框
// 只有在菜单栏点击后才显示对话框
if(checked){
ui->action_Preference->setChecked(false);
pre->show();
}else{
pre->deleteLater();
}
}

读入文件

1
2
setting = new QSettings(QCoreApplication::applicationDirPath()+"/config.ini", QSettings::IniFormat); // ini文件
readSetting();

声明私有变量:

1
2
3
4
5
6
7
8
9
10
11
12
13
private:
Ui::Setting *ui;
QSettings *setting;
QButtonGroup *setTabPosBtnGroup, *setTabShapeBtnGroup;
QRgb textColor, currentRowColor, backgroundColor, textBackgroundColor;
bool isModified;
void InitRadioButton();
void readSettings();
void sendSignal();
int lang, theme;
int showMenu,showToolBar, showStatusBar;
QString newFileName, newFileSuffix;
int setTabsMovable, setTabsClosable, tabPosition, tabShape;

读取值

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
void Setting::readSetting(){
QStringList all = setting->childGroups();
// 读取第一组数据
setting->beginGroup(all[0]);
lang = setting->value("Language").toInt();
showMenu = setting->value("ShowMainWindowMenu").toInt();
showToolBar = setting->value("ShowMainWindowToolBar").toInt();
showStatusBar = setting->value("ShowMainWindowStatusBar").toInt();
theme = setting->value("Theme").toInt();
setting->endGroup();
// 更新选项
ui->comboBox_2->setCurrentIndex(lang);
ui->cb_showMainMenu->setCheckState(Qt::CheckState(showMenu));
ui->cb_showToolBar->setCheckState(Qt::CheckState(showToolBar));
ui->cb_showStatusBar->setCheckState(Qt::CheckState(showStatusBar));
ui->cb_theme->setCurrentIndex(theme);

// 读取第二组数据
setting->beginGroup(all[1]);
newFileName = setting->value("NewFileName").toString();
newFileSuffix = setting->value("NewFileSuffix").toString();
textColor = setting->value("textColor").toInt();
currentRowColor = setting->value("currentRowColor").toInt();
backgroundColor = setting->value("backgroundColor").toInt();
textBackgroundColor = setting->value("textBackgroundColor").toInt();
setTabsMovable = setting->value("setTabsMovable").toInt();
setTabsClosable = setting->value("setTabsClosable").toInt();
tabPosition = setting->value("TabPosition").toInt();
tabShape = setting->value("TabShape").toInt();
setting->endGroup();
// 更新单选框
switch (tabPosition) {
case -2:
ui->rb_north->setChecked(true);
break;
case -3:
ui->rb_south->setChecked(true);
break;
case -4:
ui->rb_east->setChecked(true);
break;
case -5:
ui->rb_west->setChecked(true);
break;
default:
break;
}
switch (tabShape) {
case -2:
ui->rb_Rounded->setChecked(true);
break;
case -3:
ui->rb_Triangular->setChecked(true);
break;
default:
break;
}

设置主窗口状态为.ini读入的设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Setting::sendSignal(){
emit changeLang(lang);
emit showMainMenuSignal(showMenu);
emit showToolBarSignal(showToolBar);
emit showStatusBarSignal(showStatusBar);
emit setThemeSignal(theme);

emit newFileNameSignal(newFileName);
emit newFileSuffixSignal(newFileSuffix);
emit setTabsMovableSignal(setTabsMovable);
emit setTabsClosableSignal(setTabsClosable);
emit setTabPositionSignal(tabPosition);
emit setTabShapeSignal(tabShape);
}

保存设置

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

void Setting::on_buttonBox_accepted()
{
if(isModified){
setting->clear();
setting->beginGroup("Environment");
setting->setValue("Language", ui->comboBox_2->currentIndex());
setting->setValue("ShowMainWindowMenu", ui->cb_showMainMenu->checkState());
setting->setValue("ShowMainWindowToolBar", ui->cb_showToolBar->checkState());
setting->setValue("ShowMainWindowStatusBar", ui->cb_showStatusBar->checkState());
setting->setValue("Theme", ui->cb_theme->currentIndex());
setting->endGroup();
setting->beginGroup("textEditor");
setting->setValue("NewFileName", ui->le_newFileName->text());
setting->setValue("NewFileSuffix", ui->le_newFileSuffix->text());
setting->setValue("textColor", textColor);
setting->setValue("currentRowColor", currentRowColor);
setting->setValue("backgroundColor", backgroundColor);
setting->setValue("textBackgroundColor", textBackgroundColor);
setting->setValue("setTabsMovable", ui->cb_setTabsMovable->checkState());
setting->setValue("setTabsClosable", ui->cb_setTabsClosable->checkState());
setting->setValue("setTabsMovable", ui->cb_setTabsMovable->checkState());
setting->setValue("TabPosition", setTabPosBtnGroup->checkedId());
setting->setValue("TabShape", setTabShapeBtnGroup->checkedId());
setting->endGroup();
}
this->close();
}