1

I am writing data to a config file using the following code.

   QSettings settings("/root/configFile.ini",QSettings::IniFormat);

   QString userName = lineEditUsername.text();
   QString password = lineEditPassword.text();

   QList<QString> listUsername;
   QList<QString> listPassword;

   settings.beginWriteArray("UserData");

   for(i=0;i<listUsername.size();i++)
  {
      Qstring user = listUsername.at(i);         
      Qstring pass = listPassword.at(i);

      settings.setArryIndex(i);
      settings.setValue("Username",user);
      settings.setValue("Password",pass);
   }

  settings.endArray();
 }

Now when I run the code first time and give 4 or 5 values they are formed in proper order in the file. However if I run the application for second time the values start overwriting from first position. Can some one suggest me some solution for this?

Sid411
  • 703
  • 2
  • 9
  • 25

2 Answers2

3

Instead of creating and maintaining arrays and indexes, I would propose to create user credentials map and store it in the settings file as follows:

QSettings settings("/root/configFile.ini", QSettings::IniFormat);

QString userName = lineEditUsername.text();
QString password = lineEditPassword.text();

QList<QString> listUsername;
QList<QString> listPassword;

//settings.beginWriteArray("UserData");

QVariantMap userDataMapping;

for(int i = 0; i < listUsername.size() ; i++)
{
    QString user = listUsername.at(i);         
    QString pass = listPassword.at(i);

    userDataMapping[user] = pass;

    //settings.setArryIndex(i);
    //settings.setValue("Username",user);
    //settings.setValue("Password",pass);
}

// Store the mapping.
settings.setValue("UserData", userDataMapping);

//settings.endArray();
// ...

This will store your data in ini file in the following format:

UserData=@Variant(\0\0\0\b\0\0\0\x1\0\0\0\x6\0\x64\0\x64\0\x64\0\0\0\n\0\0\0\x6\0\x62\0\x62\0\x62)

When you read settings, do something like this:

[..]
QVariant v = settings.value("UserData");
QVariantMap map = v.value<QVariantMap>();
QMapIterator<QString, QVariant> i(map);
while (i.hasNext()) {
    i.next();
    QString user = i.key();
    QString pass = i.value().toString();
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • But if I have more data to add to the config file like their roles I wont be able to add it right? – Sid411 Sep 23 '13 at 05:32
  • @user2548968, I don't understand you. Please clarify your question. What is 'roles' and what kind of data are you going to add? – vahancho Sep 23 '13 at 06:28
  • I tried using this method. I wanted to add extra functionality so I took one more QVariant and did the same thing for it. However while reading it is getting unexpectedly finished. – Sid411 Sep 23 '13 at 07:24
  • Roles is like suppose its a school database and a student is logging in then he will have to define role as Student and similarly for teacher and principal. So even that has to come through config file. I will be taking this value as QString from combo box. – Sid411 Sep 23 '13 at 07:31
  • any more suggestions buddy. Still not able to figure out why I am going wrong while reading data. Just after i.next I am adding the if condition of If(username == user) { // rest of the code } In application output it is saying item_exists() in file "opt/QtSDK/....". When I debug it gives segmentation fault at QString user = i.key(); – Sid411 Sep 23 '13 at 09:04
  • 1
    @user2548968, I have fixed the code above: `i.next()` should be called before `i.key()`. – vahancho Sep 23 '13 at 10:07
  • @Vahancho..Sorry I know I am asking after a lot of days. However I am still getting the same problem. Everytime I run the program the values that I set Over write the old values. – Sid411 Oct 10 '13 at 10:58
0

You need to retrieve the amount of existing entries before adding a new one. Something like this:

int size = settings.beginReadArray( "UserData" );
settings.endArray();

settings.beginWriteArray( "UserData" );
settings.setArrayIndex( size );  // Note: Maybe 'size - 1', not  sure

// ...
settings.endArray();

setArrayIndex( size ) will move the array index to the end and will thus no longer override an existing entry

Tim Meyer
  • 12,210
  • 8
  • 64
  • 97