首页 > 代码库 > 【Qt 】QSettings写ini配置文件

【Qt 】QSettings写ini配置文件

QSettings写ini配置文件(Qt版本5.2):

#include "inidemo.h"#include <QSettings>#include <QTextCodec>IniDemo::IniDemo(QWidget *parent)    : QMainWindow(parent){    ui.setupUi(this);    QSettings settings("setting.ini",QSettings::IniFormat);    settings.clear();    settings.beginGroup("MAIN FILE INFO");    settings.setValue("exeTitle", "1111122");    settings.setValue("curDir", QApplication::applicationDirPath());    settings.endGroup();    for (int i = 0; i < 5; ++i) {        settings.beginGroup(tr("Torrents%1").arg(i));        settings.setValue("sourceFileName", i);        settings.setValue("destinationFolder",i);        settings.setValue("uploadedBytes", i);        settings.setValue("downloadedBytes", i);        settings.setValue("resumeState", i);        settings.endGroup();    }    settings.sync();}IniDemo::~IniDemo(){}

生成的配置文件如下, so easy:

[MAIN%20FILE%20INFO]exeTitle=1111122curDir=D:/CPP_Pro/QT_prjs/IniDemo/Win32/Debug[Torrents0]sourceFileName=0destinationFolder=0uploadedBytes=0downloadedBytes=0resumeState=0[Torrents1]sourceFileName=1destinationFolder=1uploadedBytes=1downloadedBytes=1resumeState=1[Torrents2]sourceFileName=2destinationFolder=2uploadedBytes=2downloadedBytes=2resumeState=2[Torrents3]sourceFileName=3destinationFolder=3uploadedBytes=3downloadedBytes=3resumeState=3[Torrents4]sourceFileName=4destinationFolder=4uploadedBytes=4downloadedBytes=4resumeState=4