QT achieve ini file read (using the beginReadArray QSettings beginWriteArray functions and data read and write the list of nodes in press)

This system configuration: ThinkPadT570, Windows10, QT5.12.2 (QtCreater4.8.2 )
I created qt project called EditPic, 1. The following code defines the Login structure, which put the two variables userName and password; 2 defined. Login logins a type of queue, and the data padding 5; 3 by beginWriteArray manner, data is written editpic.ini logins file; 4 logins node reads data from the file editpic.ini; 5. the read data is written editpic2.ini file.
Specific code as follows:
In the class structure of the outer definition of the structure mainwindow.cpp:
struct {the Login
QString the userName;
QString password;
the Login (): the userName ( ""), password ( "")
{

}
Login(QString name,QString pwd):userName(""),password("")
{
    userName = name;
    password = pwd;
}

};
Add the following code in the constructor mainwindow.cpp:

//获取ini文件路径
QString     wstrFilePath;
#if defined(Q_OS_LINUX)//区分在linux下还是windows下
    wstrFilePath = qApp->applicationDirPath() + "/../EditPic/editpic.ini" ;//in linux
#else
    wstrFilePath = qApp->applicationDirPath() + "/../../EditPic/editpic.ini" ;//in windows,我的工程名为EditPic,二editpic.ini放在工程源文件目录下
#endif
QSettings *settings=new QSettings(wstrFilePath,QSettings::IniFormat);//用QSetting获取ini文件中的数据
QList<Login> logins;//定义一个Login类型的队列,用来放配置文件中logins节点的数据
logins.push_back(Login("name1","pw1"));
logins.push_back(Login("name2","pw2"));
logins.push_back(Login("name3","pw3"));
logins.push_back(Login("name4","pw4"));
logins.push_back(Login("name5","pw5"));
settings->clear();//清空settings中的数据
settings->beginWriteArray("logins");//通过beginWriteArray的方式将队列logins写进editpic1.ini的logins节点
for (int i = 0; i < logins.size(); ++i) {
    settings->setArrayIndex(i);//标记Array的索引
    settings->setValue("userName", logins.at(i).userName);//添加userName键的值
    settings->setValue("password", logins.at(i).password);//添加password键的值
}
settings->endArray();//结束队列的输入,与settings->beginWriteArray("logins");相对应
//通过beginReadArray的方式将ini文件中的logins节点读取到logins队列中
if(!logins.empty())
{
    logins.clear();
}
int size = settings->beginReadArray("logins");
for (int i = 0; i < size; ++i) {
    settings->setArrayIndex(i);
    Login login;
    login.userName = settings->value("userName").toString();
    login.password = settings->value("password").toString();
    logins.append(login);
}
settings->endArray();
//将从editpic.ini读取到的logins节点的值写到editpic2.ini文件中
wstrFilePath = qApp->applicationDirPath() + "/../../EditPic/editpic2.ini" ;//in windows
QSettings *settings2 = new QSettings(wstrFilePath,QSettings::IniFormat);
settings2->clear();
settings2->beginWriteArray("logins");
for (int i = 0; i < logins.size(); ++i) {
    settings2->setArrayIndex(i);
    settings2->setValue("userName", logins.at(i).userName);
    settings2->setValue("password", logins.at(i).password);
}
settings2->endArray();
settings2->setValue("global/name","editpic2.ini");//通过普通的方式将global节点下name键的值设置为"editpic2.ini"

After the software is running discovery project folder and added editpic.ini editpic2.ini file, shots are as follows:
Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43935474/article/details/89370720