简体   繁体   中英

Qt4 QSettings read array with multiple values

I have an .ini file of this form:

#init file:

[files]
fileAmount=2

file1=string1, string2, 0
file2=string1, string2, 1



//cpp file:
settings=new QSettings(QString(":resources/configuration"), QSettings::IniFormat);

int n=set("files/fileAmount").toInt();

for(int i=1; i<=n; i++){
    QStringList list=settings->value("files/file"+QString::number(i)).toStringList();
    out<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl;
}




//output:
string1 string2 0
string1 string2 1

Is there any way to set whitespaces as delimiter for QStringList instead of komma?
ie have this file:

#init file:

[files]
fileAmount=2

file1=string1 string2 0
file2=string1 string2 1

and get the same output?

Ini File:

C:\\Users\\username\\AppData\\Roaming\\MyOrg\\MyApp.ini

[files]
file1=string1 string2 0

Sample code for space delimited stringlist in ini file:

#include <QCoreApplication>
#include <QStringList>
#include <QSettings>
#include <QDebug>

void readSettings();
void writeSettings();

QStringList list;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSettings::setDefaultFormat(QSettings::IniFormat);
    qApp->setApplicationName("MyApp");
    qApp->setOrganizationName("MyOrg");

    readSettings();

    qDebug() << list;

    writeSettings();

    return a.exec();
}

void readSettings()
{
    // read a space delimited value from QSettings string
    // into a stringlist
    QSettings s;

    s.beginGroup("files");

    QString temp = s.value("file1","string1 string2 0").toString();
    list << temp.split(" ");

    s.endGroup();
}

void writeSettings()
{
    // write a space delimited value from QStringList
    QSettings s;

    s.beginGroup("files");

    s.setValue("file1",list.join(" "));

    s.endGroup();
}

Output:

("string1", "string2", "0")

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM