PDA

View Full Version : using .INI files



duma
22nd August 2011, 16:22
Hi guys,
I have a QComboBox in my window which has 2 options: Board 1 and Board2 which have their own specifications for values for variables used in my program. I want to use a .INI file to call these values. Under each board, I want to assign values to the following variables: nsine, nsquare, ntri and nmulti. I am trying to learn how to use .INI file to do this.
My ini file looks something like this:

[board1]
name = Board1

[values1]
nsine = 8.56
nsquare = 8.56
ntri = 13.8
nmulti = 8.56

[board2]
name = Board2

[values2]
nsine = 50
nsquare = 50
ntri =5
nmulti = 50

I am trying to read the .ini file above using the following code, but nothing is happening. Any help would be greatly appreciated.

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

high_flyer
22nd August 2011, 16:53
define "nothing is happening".

duma
22nd August 2011, 17:41
define "nothing is happening".

Thanks for the reply.
With the following qDebug lines, im getting the expected output:
code:

QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
qDebug() << settings.allKeys();
settings.beginGroup("values1");
const QStringList childKeys = settings.childKeys();
QHash<QString, QString> values1;
foreach (const QString &childKey, childKeys)
{qDebug() << childKey << "-->" << settings.value(childKey).toString();
values1.insert(childKey, settings.value(childKey).toString());}
settings.endGroup();
qDebug() << "values1 hash" << values1;


output:

("board1/name", "board2/name", "values1/nmulti", "values1/nsine", "values1/nsquare", "values1/ntri", "values2/nmulti", "values2/nsine", "values2/nsquare", "values2/ntri")
"nmulti" --> "8.56"
"nsine" --> "8.56"
"nsquare" --> "8.56"
"ntri" --> "13.8"
values1 hash QHash(("nsine", "8.56")("nmulti", "8.56")("ntri", "13.8")("nsquare", "8.56"))

So the values from the .ini file are being read.

My goal is to assign these values to the variables(nsine,nsquare, nmulti, ntri) that i have in my program when i choose "board1" from my comboBox. I have a combo box which has an option "board1" and when i choose "board1", I want these values(8.56, 8.56, 8.56, 13.8) to be assigned to the variables(nsine,nsquare, nmulti, ntri) in my program under the function generate_sine() which uses these variables.
So what I meant by "nothing is happening" was that the variables in my function generate_sine() aren't being assigned those values. Is there something that I am missing?

Sorry for not being more clear earlier.

high_flyer
22nd August 2011, 18:48
Where is the code where you assign the values from your hash to your variables?

duma
22nd August 2011, 19:35
Where is the code where you assign the values from your hash to your variables?

I don't have any. this is where i'm stuck because I'm not sure how to do that.

majorwoody
23rd August 2011, 11:04
I don't have any. this is where i'm stuck because I'm not sure how to do that.

You already did what you want. QSettings.value() returns QVariant which has methods like toString, toFloat ... So what's the problem?

high_flyer
23rd August 2011, 11:25
I don't have any.
So if you don't have a code to fill your combobox why are surprised your comobbox is empty?
What are you stuck on?
Populating a QComboBox is pretty straight forward..

duma
23rd August 2011, 21:09
So if you don't have a code to fill your combobox why are surprised your comobbox is empty?
What are you stuck on?
Populating a QComboBox is pretty straight forward..


Hey thanks for the reply. I read about it and came up with the the following; my ini file and my code:
signalgenerator.ini:

[values]
calibration = 8.56
calibration2 = 15


code:

QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
qDebug() << settings.allKeys();

settings.beginGroup("values");
const QStringList childKeys = settings.childKeys();
QHash<QString, QString> values;
foreach (const QString &childKey, childKeys) {

qDebug() << childKey << "->" << settings.value(childKey).toString();
values.insert(childKey, settings.value(childKey).toString());
ui->calbEdit->setText(settings.value(childKey).toString()); //displays value in my line edit
if (childKey == "calibration") { calfactor = settings.value(childKey).toFloat();break;}
if (childKey == "calibration2") { calfactor2 = settings.value(childKey).toFloat();break;}

}
settings.endGroup();
qDebug() << "values hash:" << values;




for(i = 1; i < 3; i++){
ui->comboBox->addItem("Board"+QString::number(i));

if (i = 1){
int index = ui->comboBox->findData(calfactor);
ui->comboBox->setCurrentIndex(index);
} break;

if (i = 2){
int index = ui->comboBox->findData(calfactor2);
ui->comboBox->setCurrentIndex(index);
} break;

}

I have called in the the values of calfactor and calfactor2 from an ini file. With the above code i am trying to make 2 options in the comboBox called Board1 and Board2. The problems i am having are:

-Only one option: "Board1" is showing up. "Board2" is not showing up. Im not sure why?

-Using the qdebug lines that I placed in my code. I am getting the following output:

("values/calibration", "values/calibration2")
"calibration" -> "8.56"
values hash: QHash(("calibration", "8.56"))
Why isn't the information for calibration2 not showing up?
Any help would be greatly appreciated.

marcvanriet
24th August 2011, 03:44
Hi,

Of course nothing is showing up for calibration2, in the debug log, because you have a 'break' when you encounter "calibration", so the loop is terminated.

Use == as the comparison operator in C++ instead of =

And of course only 1 entry is put in the combobox because there is a break in your loop that is always executed.

You really need to learn the syntax of C/C++ first

Regards,
Marc

duma
24th August 2011, 21:33
Thanks, i got rid of the break statements and i changed = to ==. It displays both boards now.:)
I modified my .ini file(below). I am getting two values from my .ini file, 8.56 and 15. I am trying to assign "Board1" the first value(8.56) and "Board2" the second value(15) with the code below. However, it is only assigning the second value(15) to both Board1 and Board2. Why is this?

output from the qdebug statements are below aswell.

new .ini file:

[values]
1 = 8.56
2 =15

code:
@
for(i = 1; i < 3; i++){
ui->comboBox->addItem("Board"+QString::number(i));

QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
qDebug() << settings.allKeys();//qdebug

settings.beginGroup("values");
const QStringList childKeys = settings.childKeys();
QHash<QString, QString> values;
foreach (const QString &childKey, childKeys) {

qDebug() << childKey << "->" << settings.value(childKey).toString();//qdebug
values.insert(childKey, settings.value(childKey).toString());

if (childKey.toInt() == 1) {

calfactor = settings.value(childKey).toFloat();
ui->comboBox->setItemData(0, calfactor);
qDebug()<<"index:" << ui->comboBox->itemText(1)<<" value:"<<ui->comboBox->itemData(1);//qdebug
}

if (childKey.toInt() == 2) {

calfactor = settings.value(childKey).toFloat();
ui->comboBox->setItemData(1, calfactor);
qDebug()<<"index:" << ui->comboBox->itemText(1)<<" value:"<<ui->comboBox->itemData(1);//qdebug
}
}
settings.endGroup();
qDebug() << "values hash:" << values;//qdebug

Output from qdebug statements:

("values/1", "values/2")
"1" -> "8.56"
index: "Board1" value: QVariant(float, 8.56)
"2" -> "15"
index: "Board2" value: QVariant(float, 15)
values hash: QHash(("1", "8.56")("2", "15"))

high_flyer
25th August 2011, 10:42
There are two things I don't understand:
1. you say:

However, it is only assigning the second value(15) to both Board1 and Board2. Why is this?
But the debug output you post clearly shows that each board gets a different value...
2. Are you sure the output you posted belongs to the code you posted?
I ask since both debug lines in your code are the same, using the same index ('1') but get different output??

marcvanriet
25th August 2011, 22:10
Also, there is a curly bracket missing somewhere, so this code cannot be real.

My advice : user proper indentation, keep things simple, think before you code, and improve your knowledge of C/C++ syntax.

Instead of asking "why is this" in a forum, step through your code and you will see what your code does.

Regards,
Marc