Results 1 to 12 of 12

Thread: using .INI files

  1. #1
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default using .INI files

    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:
    Qt Code:
    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
    To copy to clipboard, switch view to plain text mode 

    I am trying to read the .ini file above using the following code, but nothing is happening. Any help would be greatly appreciated.
    Qt Code:
    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("values1");
    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();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using .INI files

    define "nothing is happening".
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default Re: using .INI files

    Quote Originally Posted by high_flyer View Post
    define "nothing is happening".
    Thanks for the reply.
    With the following qDebug lines, im getting the expected output:
    code:
    Qt Code:
    1. QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
    2. qDebug() << settings.allKeys();
    3. settings.beginGroup("values1");
    4. const QStringList childKeys = settings.childKeys();
    5. QHash<QString, QString> values1;
    6. foreach (const QString &childKey, childKeys)
    7. {qDebug() << childKey << "-->" << settings.value(childKey).toString();
    8. values1.insert(childKey, settings.value(childKey).toString());}
    9. settings.endGroup();
    10. qDebug() << "values1 hash" << values1;
    To copy to clipboard, switch view to plain text mode 

    output:
    Qt Code:
    1. ("board1/name", "board2/name", "values1/nmulti", "values1/nsine", "values1/nsquare", "values1/ntri", "values2/nmulti", "values2/nsine", "values2/nsquare", "values2/ntri")
    2. "nmulti" --> "8.56"
    3. "nsine" --> "8.56"
    4. "nsquare" --> "8.56"
    5. "ntri" --> "13.8"
    6. values1 hash QHash(("nsine", "8.56")("nmulti", "8.56")("ntri", "13.8")("nsquare", "8.56"))
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using .INI files

    Where is the code where you assign the values from your hash to your variables?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default Re: using .INI files

    Quote Originally Posted by high_flyer View Post
    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.

  6. #6
    Join Date
    Feb 2010
    Posts
    18
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: using .INI files

    Quote Originally Posted by duma View Post
    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?

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using .INI files

    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..
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default Re: using .INI files

    Quote Originally Posted by high_flyer View Post
    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:
    Qt Code:
    1. [values]
    2. calibration = 8.56
    3. calibration2 = 15
    To copy to clipboard, switch view to plain text mode 

    code:
    Qt Code:
    1. QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
    2. qDebug() << settings.allKeys();
    3.  
    4. settings.beginGroup("values");
    5. const QStringList childKeys = settings.childKeys();
    6. QHash<QString, QString> values;
    7. foreach (const QString &childKey, childKeys) {
    8.  
    9. qDebug() << childKey << "->" << settings.value(childKey).toString();
    10. values.insert(childKey, settings.value(childKey).toString());
    11. ui->calbEdit->setText(settings.value(childKey).toString()); //displays value in my line edit
    12. if (childKey == "calibration") { calfactor = settings.value(childKey).toFloat();break;}
    13. if (childKey == "calibration2") { calfactor2 = settings.value(childKey).toFloat();break;}
    14.  
    15. }
    16. settings.endGroup();
    17. qDebug() << "values hash:" << values;
    18.  
    19.  
    20.  
    21.  
    22. for(i = 1; i < 3; i++){
    23. ui->comboBox->addItem("Board"+QString::number(i));
    24.  
    25. if (i = 1){
    26. int index = ui->comboBox->findData(calfactor);
    27. ui->comboBox->setCurrentIndex(index);
    28. } break;
    29.  
    30. if (i = 2){
    31. int index = ui->comboBox->findData(calfactor2);
    32. ui->comboBox->setCurrentIndex(index);
    33. } break;
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. ("values/calibration", "values/calibration2")
    2. "calibration" -> "8.56"
    3. values hash: QHash(("calibration", "8.56"))
    To copy to clipboard, switch view to plain text mode 
    Why isn't the information for calibration2 not showing up?
    Any help would be greatly appreciated.

  9. #9
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using .INI files

    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

  10. #10
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default Re: using .INI files

    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:
    Qt Code:
    1. [values]
    2. 1 = 8.56
    3. 2 =15
    To copy to clipboard, switch view to plain text mode 

    code:
    @
    Qt Code:
    1. for(i = 1; i < 3; i++){
    2. ui->comboBox->addItem("Board"+QString::number(i));
    3.  
    4. QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
    5. qDebug() << settings.allKeys();//qdebug
    6.  
    7. settings.beginGroup("values");
    8. const QStringList childKeys = settings.childKeys();
    9. QHash<QString, QString> values;
    10. foreach (const QString &childKey, childKeys) {
    11.  
    12. qDebug() << childKey << "->" << settings.value(childKey).toString();//qdebug
    13. values.insert(childKey, settings.value(childKey).toString());
    14.  
    15. if (childKey.toInt() == 1) {
    16.  
    17. calfactor = settings.value(childKey).toFloat();
    18. ui->comboBox->setItemData(0, calfactor);
    19. qDebug()<<"index:" << ui->comboBox->itemText(1)<<" value:"<<ui->comboBox->itemData(1);//qdebug
    20. }
    21.  
    22. if (childKey.toInt() == 2) {
    23.  
    24. calfactor = settings.value(childKey).toFloat();
    25. ui->comboBox->setItemData(1, calfactor);
    26. qDebug()<<"index:" << ui->comboBox->itemText(1)<<" value:"<<ui->comboBox->itemData(1);//qdebug
    27. }
    28. }
    29. settings.endGroup();
    30. qDebug() << "values hash:" << values;//qdebug
    To copy to clipboard, switch view to plain text mode 

    Output from qdebug statements:
    Qt Code:
    1. ("values/1", "values/2")
    2. "1" -> "8.56"
    3. index: "Board1" value: QVariant(float, 8.56)
    4. "2" -> "15"
    5. index: "Board2" value: QVariant(float, 15)
    6. values hash: QHash(("1", "8.56")("2", "15"))
    To copy to clipboard, switch view to plain text mode 
    Last edited by duma; 24th August 2011 at 20:43.

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using .INI files

    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??
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #12
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using .INI files

    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

Similar Threads

  1. Replies: 1
    Last Post: 15th August 2011, 23:26
  2. Replies: 9
    Last Post: 28th April 2010, 09:18
  3. Replies: 12
    Last Post: 17th June 2009, 05:34
  4. visual studio project files - adding extra files
    By luf in forum Qt Programming
    Replies: 3
    Last Post: 13th June 2008, 21:05
  5. how to save sequences of text files and sound files
    By nagpalma in forum Qt Programming
    Replies: 8
    Last Post: 3rd July 2007, 00:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.