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. Having issues with QSettings and an existing INI file,
Forum Updated to NodeBB v4.3 + New Features

Having issues with QSettings and an existing INI file,

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 2.4k Views 2 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.
  • A Offline
    A Offline
    amanill
    wrote on last edited by
    #1

    Hello,

    I am trying to use qSettings to store an ini file with some key:value pairs to access later on.

    Sample INI data:

    [CONFFILES]
    APPLICATION_START_VECTOR_LOCATION=100
    BOOTLOADER_END_ADDR=1400
    BOOTLOADER_START_ADDR=400
    FILE_FORMAT_TYPE=223D
    FILE_FORMAT_VERSION=1
    

    my code:

    QSettings settings("FirmwareConf.ini", QSettings::IniFormat);
        settings.beginGroup("CONFFILES");
        const QStringList childKeys = settings.childKeys();
        QStringList values;
        foreach (const QString &childKey, childKeys)
            values << settings.value(childKey).toString();
        settings.endGroup();
        qDebug() << "all values" << values;
    

    the qDebug is just outputting all values (), where I thought i would be seeing my key:values from the ini file.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums

      Are you sure it finds the file?

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        The path you use for QSettings is a relative path. So your application will try to open it at the same place the executable can be found. This is likely not what you want.

        Where is that file exactly located on your computer ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply
        2
        • SGaistS SGaist

          Hi and welcome to devnet,

          The path you use for QSettings is a relative path. So your application will try to open it at the same place the executable can be found. This is likely not what you want.

          Where is that file exactly located on your computer ?

          A Offline
          A Offline
          amanill
          wrote on last edited by amanill
          #4

          @SGaist

          the ini is in the Other files folder of the Project

          alt text

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            That's a virtual view, not a reflection of your filesystem.

            Based on that, I guess that the file is in the root folder of your project, correct ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            A 1 Reply Last reply
            2
            • SGaistS SGaist

              That's a virtual view, not a reflection of your filesystem.

              Based on that, I guess that the file is in the root folder of your project, correct ?

              A Offline
              A Offline
              amanill
              wrote on last edited by
              #6

              @SGaist

              that is correct

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Then use the full path to the file if you want it to be properly loaded.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                A 1 Reply Last reply
                1
                • SGaistS SGaist

                  Then use the full path to the file if you want it to be properly loaded.

                  A Offline
                  A Offline
                  amanill
                  wrote on last edited by
                  #8

                  @SGaist Thanks that did the trick!

                  How would this effect the exe if it's sent to another system, does the INI file get included in the built exe?

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #9

                    Hi
                    Just as a note:
                    if you later deploy the app , using a fixed path is often not optimal.
                    There is also the option to use
                    http://doc.qt.io/qt-5/qstandardpaths.html

                    and simply put the file there in one of the locations to
                    ensure it works on other system.

                    ps. did you mean to embed the ini file into the exe ?

                    A 1 Reply Last reply
                    3
                    • mrjjM mrjj

                      Hi
                      Just as a note:
                      if you later deploy the app , using a fixed path is often not optimal.
                      There is also the option to use
                      http://doc.qt.io/qt-5/qstandardpaths.html

                      and simply put the file there in one of the locations to
                      ensure it works on other system.

                      ps. did you mean to embed the ini file into the exe ?

                      A Offline
                      A Offline
                      amanill
                      wrote on last edited by
                      #10

                      @mrjj i'm very new to QT, let alone c++ so i am not very familiar with best/proper practices. I want the ini to be accessible if the executable gets distributed, what's best practice for that?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Since you are likely going to read and write that ini file, one technique is to have one with default values embedded in your application using Qt's resource system. Then at the start of your application, copy it over the correct location using QStandardPaths and then you can access the file from your application.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hi
                          To implement what @SGaist suggest you would do the following:
                          1:
                          Make sure the file is added to the resource file
                          Right click the .qrc in project view and select Add existing file
                          alt text

                          Make sure it lo0ks something like in sample and not listed under "other files."
                          2:
                          copy it to location when run. I used document folder. there are others if u wish.

                          #include "mainwindow.h"
                          #include <QApplication>
                          #include <QSettings>
                          #include <QDebug>
                          #include <qstandardpaths.h>
                          #include <QFile>
                          
                          int main(int argc, char* argv[]) {
                          
                            QApplication a(argc, argv);
                            MainWindow w;
                            w.show();
                          
                          
                            const QString IniFileName("FirmwareConf.ini");
                            // ask where doc folder is
                            QString DocFolder = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
                            // create a new string with full path and filename
                            QString InfilePathAndFileName = DocFolder + "/" + IniFileName;
                          
                            // copy file from res file to writable location. the ":/" is due to it being inside qres file.
                            QFile::copy(":/" + IniFileName, InfilePathAndFileName  );
                            // output in Creators message pane where we copy.  ( so we can debug if doesnt work)
                            qDebug() << "Copying inifile to " << InfilePathAndFileName;
                          
                            // load it from new location
                            QSettings settings(InfilePathAndFileName, QSettings::IniFormat);
                            settings.beginGroup("CONFFILES");
                            const QStringList childKeys = settings.childKeys();
                            QStringList values;
                            foreach (const QString& childKey, childKeys) {
                              values << settings.value(childKey).toString();
                            }
                            settings.endGroup();
                          
                            qDebug() << "all values" << values;
                          
                          
                            return a.exec();
                          }
                          
                          
                          1 Reply Last reply
                          2

                          • Login

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