August 19, 2011

ogopa ogopa
Lab Rat
87 posts

using .INI file[SOLVED]

Page  
1

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. Any help would be greatly appreciated.
My ini file looks something like this:

  1. [board]
  2. name = Board1
  3.  
  4. [values]
  5. nsine = 8.56
  6. nsquare = 8.56
  7. ntri = 13.8
  8. nmulti = 8.56
  9.  
  10. [board]
  11. name = Board2
  12.  
  13. [values]
  14. nsine = 10.5
  15. nsquare = 10.5
  16. ntri = 15.6
  17. nmulti = 10.5

32 replies

August 19, 2011

Andre Andre
Area 51 Engineer
6031 posts

QSettings can work with .ini files.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

August 19, 2011

mlong mlong
Mad Scientist
1517 posts

Please don’t double post. I merged the two threads.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 19, 2011

ogopa ogopa
Lab Rat
87 posts

Sorry, it disappeared from the list so I thought it hadn’t been posted successfully.
Thanks, i am going to read on QSettings :)

August 19, 2011

Andre Andre
Area 51 Engineer
6031 posts

ogopa wrote:
Sorry, it disappeared from the list so I thought it hadn’t been posted successfully.
Thanks, i am going to read on QSettings :)

Nope, I moved it from Tools to General & Desktop, and I think I checked that notification was to be send to you about that.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

August 19, 2011

ogopa ogopa
Lab Rat
87 posts

Andre wrote:

ogopa wrote:
Sorry, it disappeared from the list so I thought it hadn’t been posted successfully.
Thanks, i am going to read on QSettings :)

Nope, I moved it from Tools to General & Desktop, and I think I checked that notification was to be send to you about that.

Yes, sorry, i got it . i just didn’t have my email open at the time. oops :)

August 22, 2011

ogopa ogopa
Lab Rat
87 posts

Hi guys, so I have a code like belowto read my .ini file, but nothing is happening. Can someone please help.

  1. QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
  2.     settings.beginGroup("values1");
  3.     const QStringList childKeys = settings.childKeys();
  4.     QHash<QString, QString> values;
  5.     foreach (const QString &childKey, childKeys)
  6.         values.insert(childKey, settings.value(childKey).toString());
  7.     settings.endGroup();
  8. settings.beginGroup("values2");
  9.     const QStringList childKeys = settings.childKeys();
  10.     QHash<QString, QString> values;
  11.     foreach (const QString &childKey, childKeys)
  12.         values.insert(childKey, settings.value(childKey).toString());
  13.     settings.endGroup();

August 22, 2011

Volker Volker
Robot Herder
5428 posts

Define “nothing is happening” please and paste the ini file you’re trying to read.

August 22, 2011

Eus Eus
Lab Rat
121 posts

Do you use the same INI file you post earlier?
And what is not “happening”?

qDebug() is your friend! Check if the file is loaded and you can retrieve data with settings.value().toString();

a simple

  1.  qDebug() << settings.value("KEYNAME").toString();

will help you determine if the file is loaded and you can get the data out of it.

In my opinion, check if it loads the file and if you can get any value out of it (even if you have to hard code it, instead of picking it up from the childkeys list), for example (if you still use that INI file which I think wont work):

  1. qDebug() << settings.value("values/nsine").toString();

Edit: Don’t forget to #include <QDebug>

August 22, 2011

ogopa ogopa
Lab Rat
87 posts
Volker wrote:
Define “nothing is happening” please and paste the ini file you’re trying to read.

Hi, thanks for the reply. The .ini file is below. What i meant by nothing is happening was that the values I am trying to read are not being read into my program. What I am trying to do is:
I have a QComboBox in my window which has 2 options: Board 1 and Board2 which have their own specifications for values for variables(nsine, nsquare, ntri and nmulti) 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.
Sorry for not being more clear earlier.

signalgenerator.ini:

  1. [board1]
  2. name = Board1
  3.  
  4. [values1]
  5. nsine = 8.56
  6. nsquare = 8.56
  7. ntri = 13.8
  8. nmulti = 8.56
  9.  
  10. [board2]
  11. name = Board2
  12.  
  13. [values2]
  14. nsine = 50
  15. nsquare = 50
  16. ntri =5
  17. nmulti = 50

August 22, 2011

ogopa ogopa
Lab Rat
87 posts

Eus wrote:
Do you use the same INI file you post earlier?
And what is not “happening”?

qDebug() is your friend! Check if the file is loaded and you can retrieve data with settings.value().toString();

a simple

  1.  qDebug() << settings.value("KEYNAME").toString();

will help you determine if the file is loaded and you can get the data out of it.

In my opinion, check if it loads the file and if you can get any value out of it (even if you have to hard code it, instead of picking it up from the childkeys list), for example (if you still use that INI file which I think wont work):

  1. qDebug() << settings.value("values/nsine").toString();

Edit: Don’t forget to #include <QDebug>

Hi, Thanks for the reply. By nothing is happening, i mean that the variables in my program are not being assigned the values from the .ini file.
i used qdebug() and it showed me that there was no value for nsine or any of the other variables. Why is that? what am i doing wrong?

August 22, 2011

Eus Eus
Lab Rat
121 posts

I think that your problem has something to do with the location of your file.
I am not sure if QSettings doesn’t require some relative path to the working dir, or is it permissions problem?
Can you try writing a value to the file, then retrieve it? QSettings should create the file if it doesn’t exist, so maybe check if you do not have another file with the same name?

August 22, 2011

Volker Volker
Robot Herder
5428 posts

Seems that your file cannot be accessed or something similar. This code works for me:

  1. QSettings settings("/tmp/qdn-test.ini", QSettings::IniFormat);
  2. qDebug() << settings.allKeys();
  3.  
  4. settings.beginGroup("values1");
  5. const QStringList childKeys = settings.childKeys();
  6. QHash<QString, QString> values1;
  7. foreach (const QString &childKey, childKeys) {
  8.     qDebug() << childKey << "-->" << settings.value(childKey).toString();
  9.     values1.insert(childKey, settings.value(childKey).toString());
  10. }
  11. settings.endGroup();
  12. qDebug() << "values1 hash:" << values1;

The out put is as expceted:

  1. ("board1/name", "board2/name", "values1/nmulti", "values1/nsine", "values1/nsquare", "values1/ntri", "values2/nmulti", "values2/nsine", "values2/nsquare", "values2/ntri")
  2.  
  3. "nmulti" --> "8.56"
  4. "nsine" --> "8.56"
  5. "nsquare" --> "8.56"
  6. "ntri" --> "13.8"
  7.  
  8. values1 hash: QHash(("nsine", "8.56")("nmulti", "8.56")("ntri", "13.8")("nsquare", "8.56"))

August 22, 2011

ogopa ogopa
Lab Rat
87 posts
Eus wrote:
I think that your problem has something to do with the location of your file. I am not sure if QSettings doesn’t require some relative path to the working dir, or is it permissions problem? Can you try writing a value to the file, then retrieve it? QSettings should create the file if it doesn’t exist, so maybe check if you do not have another file with the same name?

Hi, I double checked my file path and it seems to be correct. I checked and there is no other file with the same name.

Earlier, you said:
“if you still use that INI file which I think wont work”
Why is that?

August 22, 2011

Eus Eus
Lab Rat
121 posts

ogopa wrote:

My ini file looks something like this:

  1. [board]
  2. name = Board1
  3.  
  4. [values]
  5. nsine = 8.56
  6. nsquare = 8.56
  7. ntri = 13.8
  8. nmulti = 8.56
  9.  
  10. [board]
  11. name = Board2
  12.  
  13. [values]
  14. nsine = 10.5
  15. nsquare = 10.5
  16. ntri = 15.6
  17. nmulti = 10.5

same group names, same key names?
if you try to access it, which value it should get?

August 22, 2011

Volker Volker
Robot Herder
5428 posts

ogopa wrote:

Hi, I double checked my file path and it seems to be correct. I checked and there is no other file with the same name.

Did you try to open the file with QFile in your Qt app, read the contents and check what’s in it?

  1. QFile iniFile("/tmp/qdn-test.ini");
  2. QFileInfo iniFileFI(iniFile);
  3. if(iniFile.exists()) {
  4.     if(iniFile.open(QIODevice::ReadOnly)) {
  5.         QString contents(iniFile.readAll());
  6.         qDebug() << contents;
  7.  
  8.     } else {
  9.         qDebug() << "ERROR: file cannot be openend";
  10.         qDebug() << "Error message is:" << iniFile.errorString();
  11.     }
  12.  
  13. } else {
  14.     qDebug() << "ERROR: file does not exist.";
  15. }

Page  
1

  ‹‹ QVideo and AVI Formats      Multicolored text for tree/table/list items? ››

You must log in to post a reply. Not a member yet? Register here!