QT读写ini配置文件超级简便写法示例
头文件需包含QSettings
#include <QSettings>
mainwindow.h
//path为ini配置文件路径,info为写入内容,dst_name为取出结果
void write_ini(QString section , QString node , QString info ,QString path);//写ini
void read_ini(QString section , QString node , QString &dst_name ,QString path);//读ini
mainwindow.cpp
void MainWindow::write_ini(QString section, QString node, QString info, QString path)
{
QSettings iniwrite(path,QSettings::IniFormat);
QString str;
str.clear();
str.append("/");
str.append(section);
str.append("/");
str.append(node);
iniwrite.setValue(str,info);
}
void MainWindow::read_ini(QString section, QString node, QString &dst_name, QString path)
{
QSettings iniread(path,QSettings::IniFormat);
QString str;
str.clear();
str.append("/");
str.append(section);
str.append("/");
str.append(node);
dst_name = iniread.value(str).toString();
}
调用示例
QString str;
write_ini("student","name","张三","D:/aa.ini");
read_ini("teacher","name",str,"D:/aa.ini");
qDebug()<<str;