Qt之读取配置文件

一、读取配置文件增删功能与修改参数数据

1 #ifndef CONFIG_H
2 #define CONFIG_H
3 
4 #define QS_FILEPATH "E:\woo\Code\Qt\APP_002_READCONF\config.ini"
5 
6 #endif // CONFIG_H
View Code
 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include "config.h"
 6 #include <QSettings>
 7 #include <QDebug>
 8 namespace Ui {
 9 class MainWindow;
10 }
11 
12 class MainWindow : public QMainWindow
13 {
14     Q_OBJECT
15 
16 public:
17     explicit MainWindow(QWidget *parent = 0);
18     ~MainWindow();
19 
20 private:
21     Ui::MainWindow *ui;
22 
23     QSettings *qs;
24     bool P01_FUN1;
25     bool P02_FUN2;
26     QString P03_Passwd;
27 
28     void F_Read_Config();
29     void F_Exec_Fun1();
30     void F_Exec_Fun2();
31 };
32 
33 #endif // MAINWINDOW_H
View Code
 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent) :
 5     QMainWindow(parent),
 6     ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9     qs = new QSettings(QS_FILEPATH, QSettings::IniFormat);
10     F_Read_Config();
11     if(P01_FUN1)
12     {
13         F_Exec_Fun1();
14     }
15     if(P02_FUN2)
16     {
17         F_Exec_Fun2();
18     }
19 }
20 
21 MainWindow::~MainWindow()
22 {
23     delete ui;
24 }
25 void MainWindow::F_Read_Config()
26 {
27     P01_FUN1 = qs->value("P01_FUN1",true).toBool();
28     if(P01_FUN1)
29     {
30         P01_FUN1 = true;
31     }
32 
33     P02_FUN2 = qs->value("P02_FUN2",true).toBool();
34     if(P02_FUN2)
35     {
36         P02_FUN2 = true;
37     }
38 
39     P03_Passwd = qs->value("P03_Passwd","313131313131").toString();
40     if(P03_Passwd.length() == 0)
41     {
42         P03_Passwd = "313131313131";
43     }
44 }
45 void MainWindow::F_Exec_Fun1()
46 {
47     qDebug(".................[fun1]");
48 }
49 void MainWindow::F_Exec_Fun2()
50 {
51     qDebug()<<".................[fun2] Passwd = "+P03_Passwd;
52 }
View Code
原文地址:https://www.cnblogs.com/pokerface/p/6263943.html