PDA

View Full Version : QSettings problem



eleanor
4th May 2009, 10:59
Hi, I have a problem writting and reading QSettings...



void clientGui::read_settings() {
QSettings settings(COMPANY, PRODUCT);
tab_counter = settings.value("counter").toInt();
for(quint32 i=0;i<tab_counter;i++) {
tab_names.append(settings.value("tab"+tab_counter).toString());
}
}

void clientGui::write_settings() {
/* TODO: spremeni ti dve imeni v dejanski imeni: ime_firme : ime_programa */
QSettings settings(COMPANY, PRODUCT);

/* counter - number of additional tabs */
settings.setValue("counter", tab_counter);

/* save the schema names */
settings.setValue("tab"+tab_counter, tab_name);
}

void clientGui::change_settings(QString name, bool read_write) {
QSettings settings(COMPANY, PRODUCT);

/* i++ the counter - add one new tab */
if(read_write) {
tab_counter += 1;
/* save the tab name */
settings.setValue("tab"+tab_counter, name);
}
else if(tab_counter > 0) {
tab_counter -= 1;

/* remove the name */
settings.remove(name);
}
/* save the counter */
settings.setValue("counter", tab_counter);

}



The problem is that my settings look like this:


[General]
ab=fadf
counter=0
b=erf
tab=


where it should look like this:
counter=0
tab0=X
tab1=X
tab2=X ... etc (where X is some name)

change_settings should add new tab[number] variable with a name...

What is the problem...any ideas?

Lykurg
4th May 2009, 13:01
settings.setValue("tab"+tab_counter, tab_name);
Instead of doing it that way, you might have a look at QSettings::beginWriteArray().

mcosta
4th May 2009, 14:16
Probably the Error is that QString::operator+ doesn't accept numeric values.
You can use


settings.setValue("tab" + QString::number(tab_counter), tab_name);

or


settings.setValue(QString("tab").arg(tab_counter), tab_name);

faldzip
4th May 2009, 14:30
what's more, this code:


for(quint32 i=0;i<tab_counter;i++) {
tab_names.append(settings.value(QString("tab%1").arg(tab_counter)).toString());
}

for 10 tabs will append the "tab10" 10 times, because in every loop pass tab_counter is constant and equal 10, so I think you want to put i in arg() and not tab_counter.

Lykurg
4th May 2009, 14:34
settings.setValue(QString("tab%1").arg(tab_counter), tab_name);

;)

mcosta
4th May 2009, 15:11
Sorry!!

But this


settings.setValue(QString("tab%1").arg(i), tab_name);

can be better
:D