PDA

View Full Version : having problems adding text to .ini file in the correct place



duma
29th August 2011, 18:04
I am having problems saving a line to my .ini file but it is not being sve din the place I want it to. I am trying to change it from:

[values]
1=8.56
2=10

[def]
def=1

to this:

[values]
1=8.56
2=10
3=10

[def]
def=1

but instead, I'm getting this:

[values]
1=8.56
2=10

[def]
def=1

3=10

I want the line 3=10 to be placed under [values], not under [def]. What am I doing wrong?

The code I have is below. The code takes the count of values in my combo box :
ui->comboBox->count(); and increases it by 1 (called "i" in the code. Then takes in a value written in a lineEdit called "newCalNumberAdd" and writes the line to the .ini file:
write<<"\n"<<i<<"="<<ui->newCalNumberAdd->text();



void wave::on_addboardButton_clicked()
{
QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
settings.beginGroup("values");
const QStringList childKeys = settings.childKeys();
QHash<QString, QString> values;
foreach (const QString &childKey, childKeys) {
values.insert(childKey, settings.value(childKey).toString());
if (childKey.toInt() == ui->comboBox->currentIndex()+1) {
ui->calnumber->setText(settings.value(childKey).toString());

QFile file("/home/test/Documents/Wave/signalgenerator.ini");
file.open(QIODevice::ReadWrite| QIODevice::Text);

QTextStream write(&file);
QString line = write.readLine();

while (!line.isNull()){
int i = ui->comboBox->count();
i += childKey.toInt();
write<<"\n"<<i<<"="<<ui->newCalNumberAdd->text();
ui->comboBox->addItem("Board"+QString::number(i));
break;
}

file.close();
}

}
settings.endGroup();
}

mvuori
29th August 2011, 18:08
.ini files are not meant or designed to be "written" in. QSettings is a class designed to save data in .ini files.

duma
29th August 2011, 18:12
.ini files are not meant or designed to be "written" in. QSettings is a class designed to save data in .ini files.

Yes, that is what I meant though. I want to save this line in the .ini file. But it is not being placed where I want. i am using QSettings :)

srazi
29th August 2011, 22:39
The problem is you open INI file in ReadWrite mode and write to it! this appends data to the end of file, you should seek to appropriated position and then write to it!

BUT you don't need that way, just use QSettings for writing data to ini file:


void wave::on_addboardButton_clicked()
{
QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
settings.beginGroup("values");
const QStringList childKeys = settings.childKeys();
QHash<QString, QString> values;
foreach (const QString &childKey, childKeys) {
values.insert(childKey, settings.value(childKey).toString());

if (childKey.toInt() == ui->comboBox->currentIndex()+1) {
ui->calnumber->setText(settings.value(childKey).toString());

int i = ui->comboBox->count()+childKey.toInt();
settings.setValue(QString::number(i), ui->newCalNumberAdd->text());
ui->comboBox->addItem("Board"+QString::number(i));
}
}

settings.endGroup();
}