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
有帮助吗?

解决方案

Problem stemmed from the INI not being created correctly.

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

其他提示

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().

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top