Using .INI file[SOLVED]



  • 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:

    @[board]
    name = Board1

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

    [board]
    name = Board2

    [values]
    nsine = 10.5
    nsquare = 10.5
    ntri = 15.6
    nmulti = 10.5@



  • [[Doc:QSettings]] can work with .ini files.


  • Moderators

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



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



  • [quote author="ogopa" date="1313769848"]Sorry, it disappeared from the list so I thought it hadn't been posted successfully.
    Thanks, i am going to read on QSettings :)[/quote]
    Nope, I moved it from Tools to General & Desktop, and I think I checked that notification was to be send to you about that.



  • [quote author="Andre" date="1313774086"]
    [quote author="ogopa" date="1313769848"]Sorry, it disappeared from the list so I thought it hadn't been posted successfully.
    Thanks, i am going to read on QSettings :)[/quote]
    Nope, I moved it from Tools to General & Desktop, and I think I checked that notification was to be send to you about that.

    [/quote]

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



  • Hi guys, so I have a code like belowto read my .ini file, but nothing is happening. Can someone please help.
    @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("values2");
    const QStringList childKeys = settings.childKeys();
    QHash<QString, QString> values;
    foreach (const QString &childKey, childKeys)
    values.insert(childKey, settings.value(childKey).toString());
    settings.endGroup(); @



  • Define "nothing is happening" please and paste the ini file you're trying to read.



  • 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
    [code]
    qDebug() << settings.value("KEYNAME").toString();
    [/code]
    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):

    [code]
    qDebug() << settings.value("values/nsine").toString();
    [/code]

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



  • [quote author="Volker" date="1314023430"]Define "nothing is happening" please and paste the ini file you're trying to read.[/quote]

    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:
    @[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
    @



  • [quote author="Eus" date="1314023593"]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
    [code]
    qDebug() << settings.value("KEYNAME").toString();
    [/code]
    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):

    [code]
    qDebug() << settings.value("values/nsine").toString();
    [/code]

    Edit: Don't forget to #include <QDebug>[/quote]

    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?



  • 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?



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

    @
    QSettings settings("/tmp/qdn-test.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;
    @

    The out put is as expceted:

    @
    ("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"))
    @



  • [quote author="Eus" date="1314024570"]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?[/quote]

    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?



  • [quote author="ogopa" date="1313765413"]
    My ini file looks something like this:

    @[board]
    name = Board1

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

    [board]
    name = Board2

    [values]
    nsine = 10.5
    nsquare = 10.5
    ntri = 15.6
    nmulti = 10.5@

    [/quote]

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



  • [quote author="ogopa" date="1314025273"]

    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.
    [/quote]

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

    @
    QFile iniFile("/tmp/qdn-test.ini");
    QFileInfo iniFileFI(iniFile);
    if(iniFile.exists()) {
    if(iniFile.open(QIODevice::ReadOnly)) {
    QString contents(iniFile.readAll());
    qDebug() << contents;

    } else {
        qDebug() << "ERROR: file cannot be openend";
        qDebug() << "Error message is:" << iniFile.errorString();
    }
    

    } else {
    qDebug() << "ERROR: file does not exist.";
    }
    @



  • [quote author="Volker" date="1314024957"]Seems that your file cannot be accessed or something similar. This code works for me:

    @
    QSettings settings("/tmp/qdn-test.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;
    @

    The out put is as expceted:

    @
    ("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"))
    @
    [/quote]

    Thanks, i am getting the same output now. But the variables in my program are not being assigned those respective values.



  • I would bet a penny that you're screwed up with some scoping issues and your hash variables are destroyed on leaving the code that reads the settings.



  • Yeah, I agree with Volker, maybe give us some more of your code, to make sure you are not getting out of the scope. That would have been my next guess as well.



  • a



  • How is that last code snippet related to your QSettings problem? I don't see any QSettings calls in that and my my crystal ball is out of order right now...

    I strongly suggest you put your hands off that wave generation stuff immediately and learn the very basic stuff of programming in general and the details of C++ in particular.

    Please don't feel offended, but I really do have the impression that you lack a good bunch of basic knowledge how all this pointers, references, classes, member variables, local variables, scopes etc.pp. play together. And to put a nice complication on top you're using C external stuff in your app. Please step back from that for a while, learn the mentioned basics and then get back to your wave generation project. It will save you from many more frustrating moments and you will have much more fun continuing your current project once you can focus on the actual stuff than on the C/C++ clutters...



  • Sorry guys for the previous unrelated post. I got rid of it. So I took some time to read up a little more and I came up with this: my .ini file and my code:

    signalgenerator.ini:
    [CODE][values]
    calibration = 8.56
    calibration2 = 15
    [/CODE]

    code:
    [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;
    

    }
    [/CODE]
    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:
    CODE
    "calibration" -> "8.56"
    values hash: QHash(("calibration", "8.56")) [/CODE]
    Why isn't the information for calibration2 showing up?
    Any help would be greatly appreciated.



  • @
    if (i = 1){
    int index = ui->comboBox->findData(calfactor);
    ui->comboBox->setCurrentIndex(index);
    } break;
    @
    You break the loop before the int i can get another value. You loop is poorly constructed as well.....

    In my opinion you should listen to what Volker said and go back to some more basic stuff first.....



  • [quote author="Eus" date="1314127853"]@
    if (i = 1){
    int index = ui->comboBox->findData(calfactor);
    ui->comboBox->setCurrentIndex(index);
    } break;
    @
    You break the loop before the int i can get another value. In my opinion you should listen to what Volker said and go back to some more basic stuff first.....[/quote]

    Thanks, i saw that and had fixed it. But, it wouldn't work anyway because of the first problem i stated which was, there is no option for "Board2" being added to my QComboBox, only "Board1". I'm confused as to why that is because my loop says from 1 till less than 3, which should include Board1 and Board2.


  • Moderators

    You have two errors in the code:

    The "break" statements are exiting the loop early

    You are using if (i = 1) instead of if (i == 1) in your if statements.



  • I am actually amazed it works. Do you have the variable i declared somewhere in the app? Because of your "for" loop, your app shouldn't start at all (having "i" with no type).
    I think your app does exactly what your code suggests it to do.

    P.S. There is a better way to get the values and values count from QSettings. Read the documentation to get familiar with it.



    • What are the breaks doing on lines 12 and 13 of your code? First you exit the foreach loop, and then you complain that it doesn't read all your values?
    • Why do you set the value of your line edit for each of the values you encounter in the values group of your settings file? What is that supposed to yield you?


  • [quote author="Andre" date="1314164692"]* What are the breaks doing on lines 12 and 13 of your code? First you exit the foreach loop, and then you complain that it doesn't read all your values?

    • Why do you set the value of your line edit for each of the values you encounter in the values group of your settings file? What is that supposed to yield you?[/quote]
      Sorry, I got rid of all the breaks. Assigning the value to lineEdit was irrelevant, it just display the value gotten from the ini file into the lineEdit.

    [quote author="mlong" date="1314128549"]You have two errors in the code:

    The "break" statements are exiting the loop early

    You are using if (i = 1) instead of if (i == 1) in your if statements.[/quote]

    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")) @


  • Moderators

    Your qDebug in line 19 is identical to line 26.

    Nonetheless, I can't see from the output of your qDebug statements where things appear to be incorrect. The output you show would indicate that, despite the creative design of your .ini file, you are getting the right data into boards 1 and 2.

    As an aside, you may be better off with an ini format such as:
    @
    [Board1]
    value=8.56

    [Board2]
    value=15
    @

    And you could make a much cleaner implementation. Something like:
    @
    QSettings settings("/home/test/Documents/Wave/signalgenerator.ini",
    QSettings::IniFormat);

    QHash<QString, QString> values;

    for(i = 0; i < 2; i++) { // Let's index the loop as if we're C++ developers

      QString boardname = QString("Board%1").arg(i+1);  
    
      settings.beginGroup(boardname);
    
      // Get your value
      QString value = settings.value("value").toString();
      values.insert(QString::number(i+1),value);
      float calfactor = value.toFloat();
      
      // Add to your UI
      ui->comboBox->addItem(boardname);  // Same as the key group name, so reuse it
      ui->comboBox->setItemData(i, calfactor);
      
      settings.endGroup();
    

    }

    qDebug() << "values hash:" << values;
    @

    (this code has not been tested)



  • As an aside, you may be better off with an ini format such as:
    @
    [Board1]
    value=8.56

    [Board2]
    value=15
    @

    Thanks alot mlong, I tried your code, it still has the same problem as my old one where whether I choose Board1 or Board2, it still uses the 2nd value(15).

    Although, I did modify my code and got it working but it is ugly because I have to put my code(below) under every pushButton function that generates a wave.

    I like yours better and it would make my code alot more simple if i could get yours working so I will experiment with the code you gave me and see if I can get it working.

    the code that worked for me:
    @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) {calfactor = settings.value(childKey).toFloat();}
    }
    settings.endGroup();@


  • Moderators

    I would suspect, then, that your original problem wasn't in reading the values from the ini format, but rather where you were setting the values to the boards themselves. I don't think you had shared that code.



  • To solve the kind of questions of why a snippet of code like you show us does not work as you expect it, please learn to use a debugger. Set a breakpoint at the top of your function, and then step through your function one line at a time. It allows you to follow the programs flow through your function, thus giving you insight in why it doesn't work the way you expect it.



  • [quote author="mlong" date="1314218770"]I would suspect, then, that your original problem wasn't in reading the values from the ini format, but rather where you were setting the values to the boards themselves. I don't think you had shared that code.

    [/quote]

    Thnks for all your help so far. You are right, my problem was setting the values to the boards. sorry. I had shared all the code i had for the problem. What I did was I modified this code:
    @ 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@

    to get this one(which worked(but only if I put it under each onpushButton function) :
    @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) {calfactor = settings.value(childKey).toFloat();}
    }
    settings.endGroup();@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.