PDA

View Full Version : QSettings - beginReadArray not working



Mike
9th January 2006, 10:50
Hi all,

I have a suspicion, that the beginReadArray method of Qt 4.1 is not working. I use the QSettings sample to save the window size and location and that's working. Now I wanted to save account information as an array. I see the data in stored in the applications configuration file, but when I try to load the data using the above method, the size returned is alway's 0.

Anybody else see this?

Regards,
Mike

wysota
9th January 2006, 13:00
I used array reading capabilities and it worked fine. Could you provide a minimum compilable example which reproduces the problem?

Mike
9th January 2006, 14:56
I will do tonight. Hope you're around. Thx

Mike
9th January 2006, 15:40
Btw., did you use it under Windows or Linux? Maybe there is a difference if you use it with the Registry or a flat file under Linux. I also use all defaults - no custom storage handler...

wysota
9th January 2006, 17:16
Btw., did you use it under Windows or Linux? Maybe there is a difference if you use it with the Registry or a flat file under Linux. I also use all defaults - no custom storage handler...

All defaults, and there is no difference between platforms in this code. Array reading is a "high-level" (platform independent) call. It then calls lower-level (platform dependent) methods to read the item itself.

Mike
9th January 2006, 21:51
hmmm... I guess you are right. My little test app seems to work correctly:



#include <QSettings>
#include <QString>

void saveSettingsArray()
{
QSettings settings("MKrueger", "SettingsTest");
settings.beginWriteArray("blogAccounts");

for (int i = 0; i <= 2; ++i)
{
// This is a new account, we will save it to the end of the list ...
settings.setArrayIndex(i);
settings.setValue("accountName", "Test-Account: " +
QString::number(i));
settings.setValue("hostName", "www.michael-krueger.org");
}
settings.endArray();
}

void loadSettingsArray()
{
QSettings settings("MKrueger", "SettingsTest");
int size = settings.beginReadArray("blogAccounts");

for (int i = 0; i < size; ++i)
{
// Select the data set
settings.setArrayIndex(i);
//
QString temp("accountName: ");
temp += settings.value("accountName").toString();
qDebug(temp.toAscii());
temp = "hostName: ";
temp += settings.value("hostName").toString();
qDebug(temp.toAscii());
}
settings.endArray();
}

int main(int /*argc*/, char /**argv[]*/)
{
saveSettingsArray();
//
loadSettingsArray();
return 0;
}



The app above does work as expected. I need to double check my application.

The output is correctly:


micha@helium:~/develop/QSettingsTest> ./QSettingsTest
accountName: Test-Account: 0
hostName: www.michael-krueger.org
accountName: Test-Account: 1
hostName: www.michael-krueger.org
accountName: Test-Account: 2
hostName: www.michael-krueger.org
micha@helium:~/develop/QSettingsTest>


... very strange, very strange ...

wysota
9th January 2006, 22:19
Check if settings are correct in the stored file (if you use files).

Mike
9th January 2006, 22:24
I think I found the problem. I think that I did set the array index wrong. That could have caused my problem I guess. I will check that again. The file did contain my settings. Writing must have been ok, reading not.