我正在尝试在配置文件中搜索字符串,并且字符串匹配是否要删除键/值对。我从文件中获取QStringList。 就我的尝试的代码而言
int main(int argc, char *argv[])
{
QSettings* settings= new QSettings("/home/sidheshwar/Desktop/temp.txt", QSettings::IniFormat);
settings->beginGroup("Profiles");
const QStringList childKeys = settings->childKeys();
QStringList Keys;
QStringList values;
QString user="db-host";
QString tempUser;
foreach (const QString &childKey, childKeys)
{
Keys << childKey;
values << settings->value(childKey).toString();
}
for(int i=0;i< Keys.length();i++){
if(user == values.at(i)){
qDebug() << " keys" << Keys[i] << endl;
tempUser=Keys[i];
}
qDebug() << " tempUser" << tempUser << endl;
}
return 0;}
如何使用settings->remove(tempUser);
在以下示例中,我向您展示了如何从处理配置的文件中删除数据的示例。
temp.ini 在执行之前。
[Profiles]
key1=db-host
key2=value2
key3=value3
main.cpp
#include <QCoreApplication>
#include <QSettings>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSettings* settings= new QSettings("temp.ini", QSettings::IniFormat);
settings->beginGroup("Profiles");
const QStringList childKeys = settings->childKeys();
QStringList Keys;
QStringList values;
QString user="db-host";
foreach (const QString &childKey, childKeys)
{
Keys << childKey;
values << settings->value(childKey).toString();
}
for(int i=0;i< Keys.length();i++){
if(user == values.at(i)){
qDebug() << " keys" << Keys[i];
settings->remove(Keys[i]);
}
qDebug() << Keys[i] << values.at(i);
}
return a.exec();
}
输出:
temp.ini 执行后
[Profiles]
key2=value2
key3=value3