Pregunta

I create an INI file and then try to read it. There is no compile errors and the file is there but I'm not receiving any data in my QStringList

QFile checkConfig(configPath);
    if(checkConfig.exists())
    {
        QSettings* settings = new QSettings(configPath, QSettings::IniFormat);
        settings->beginGroup("General");
        const QStringList childKeys = settings->childKeys();
        QStringList values;
        foreach (const QString &childKey, childKeys)
            values << settings->value(childKey).toString();
        settings->endGroup();
        QString test = configPath;
        int a;
    }

this is my INI file

[General]
InterviewPath=C:/1234
¿Fue útil?

Solución

Problem stemmed from the INI not being created correctly.

QSettings settings(configPath, QSettings::IniFormat);
    settings.beginGroup("/General");
    settings.setValue("/InterviewPath", interviewPath);
    settings.endGroup();

Otros consejos

Do not use settings->beginGroup("General");

        QCoreApplication a(argc, argv);
    QSettings* settings = new QSettings("d:/a.ini", QSettings::IniFormat);
  //  settings->beginGroup("General");
    const QStringList childKeys = settings->childKeys();
    QStringList values;
    foreach (const QString &childKey, childKeys)
        values << settings->value(childKey).toString();
   // settings->endGroup();
    qDebug()<<values;
   QSettings* settings = new QSettings(configPath, QSettings::IniFormat);
   settings.sync(); // !!!

If your first call will be run after some events processing - then all will be ok. But if you want to directly open .ini file and use it in same method - then you should force syncronization. Read qt docs for additional info about sync().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top