How to properly use QSettings
Asked Answered
E

3

6

I want to use QSettings to save highscores but it doesn't work properly. I'm saving and reading those values in 2 different files.

This is my code responsible for adding values into array:

QSettings settings;
settings.beginWriteArray("results");
int size = settings.beginReadArray("results");
settings.setArrayIndex(size);
settings.setValue("result", "qwerty");

and reading:

QSettings settings;
QString tmp = "";
int size = settings.beginReadArray("results");
for(int i = 0; i < size; ++i)
{
    settings.setArrayIndex(i);
    tmp += settings.value("result").toString();
}
ui->label->setText(tmp);
Executant answered 27/4, 2013 at 10:29 Comment(0)
S
8

I would do it like this:

Lets say that we have two functions member of a class to load and save the scores. To use the registry, you have to specify the application name and editor:

QSettings settings("<MyEditorName>","<myAppName>");
saveScores(settings);
loadScores(settings);

to use a file, you have to provide the file path and format:

QSettings settings("<filepath>",QSettings::iniFormat);
saveScores(settings);
loadScores(settings);

from your code and the documentation; the member function would be as follow. The class countains a vector of scores (QVector mScores)

Function to save the scores:

void myClass::saveScores(QSettings& iSettings)
{
  iSettings.beginGroup("Scores");
  iSettings.beginWriteArray("results");
  for(int i=0; i<mScores.count();i++)
  {
    iSettings.setArrayIndex(i);
    iSettings.setValue("result",mScores[i]);
  }
  iSettings.endArray();
  iSettings.endGroup();
}

Function to load the scores

void myClass::loadScores(QSettings& iSettings)
{
  iSettings.beginGroup("Scores");
  int size = iSettings.beginReadArray("results");
  mScores.resize(size);
  for(int i=0;i<size;i++)
  {
    iSettings.setArrayIndex(i);
    mScores[i] = iSettings->value("result").toInt();
  }
  iSettings.endArray();
  iSettings.endGroup();
}

I am using groups to provide better visibility in the saving file but you can remove them

Saturate answered 27/4, 2013 at 13:44 Comment(2)
Nice answer, but you're missing iSettings->endArray()Benzocaine
mScores[i] = iSettings->value("results").toInt(); should be mScores[i] = iSettings.value("result").toInt(); in loadScores()External
E
2

The beginReadArray() after beginWriteArray() is causing the problem. Do this:

QSettings settings;
int size = settings.beginReadArray("results");
settings.endArray();
settings.beginWriteArray("results");
settings.setArrayIndex(size);
settings.setValue("result", "qwerty");
settings.endArray();

Note you need to call endArray() when finished.

Eula answered 27/4, 2013 at 13:47 Comment(0)
P
0

Using QSettings to read an ini file also showing the required format of the expected ini file\n Format of alphabet.ini :

[A_SECTION]
AA=20
BB=40
CC=0

[B_SECTION]
DD=100
EE=270
FF=3000

Simple code to read alphabet.ini :

QSettings settings("C:\\Qt\\qtcreator-2.5.2\\testingProg\\alphabet.ini",QSettings::IniFormat);

settings.beginGroup("A_SECTION");
const QStringList AchildKeys = settings.childKeys();
QHash<QString, QString> Avalues;
foreach (const QString &childKey, AchildKeys)
 {
     Avalues.insert(childKey, settings.value(childKey).toString());
     qDebug() << childKey << " : " <<settings.value(childKey).toString();
 }
settings.endGroup();
qDebug() << ";

settings.beginGroup("B_SECTION");
const QStringList BchildKeys = settings.childKeys();
QHash<QString, QString> Bvalues;
foreach (const QString &childKey, BchildKeys)
 {
     Bvalues.insert(childKey, settings.value(childKey).toString());
     qDebug() << childKey << " : " <<settings.value(childKey).toString();
 }
settings.endGroup();

The output:

"AA"  :  "20"
"BB"  :  "40"
"CC"  :  "0"

"DD"  :  "100"
"EE"  :  "270"
"FF"  :  "3000"
Plasty answered 11/8, 2021 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.