Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using .INI file[SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Using .INI file[SOLVED]

Scheduled Pinned Locked Moved General and Desktop
33 Posts 5 Posters 13.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ogopa
    wrote on last edited by
    #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:

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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #3

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

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • O Offline
          O Offline
          ogopa
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            [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.

            1 Reply Last reply
            0
            • O Offline
              O Offline
              ogopa
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • O Offline
                O Offline
                ogopa
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

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

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Robbin
                    wrote on last edited by
                    #9

                    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>

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      ogopa
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • O Offline
                        O Offline
                        ogopa
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          Robbin
                          wrote on last edited by
                          #12

                          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?

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #13

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

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0
                            • O Offline
                              O Offline
                              ogopa
                              wrote on last edited by
                              #14

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

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                Robbin
                                wrote on last edited by
                                #15

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

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  goetz
                                  wrote on last edited by
                                  #16

                                  [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.";
                                  }
                                  @

                                  http://www.catb.org/~esr/faqs/smart-questions.html

                                  1 Reply Last reply
                                  0
                                  • O Offline
                                    O Offline
                                    ogopa
                                    wrote on last edited by
                                    #17

                                    [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.

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      goetz
                                      wrote on last edited by
                                      #18

                                      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.

                                      http://www.catb.org/~esr/faqs/smart-questions.html

                                      1 Reply Last reply
                                      0
                                      • R Offline
                                        R Offline
                                        Robbin
                                        wrote on last edited by
                                        #19

                                        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.

                                        1 Reply Last reply
                                        0
                                        • O Offline
                                          O Offline
                                          ogopa
                                          wrote on last edited by
                                          #20

                                          a

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved