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. Save values to a config file

Save values to a config file

Scheduled Pinned Locked Moved Solved General and Desktop
43 Posts 7 Posters 6.8k Views
  • 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.
  • S Sucharek

    It didn't output anything

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #9

    @Sucharek said in Save values to a config file:

    It didn't output anything

    Please use the documentation to understand.

    1 Reply Last reply
    0
    • S Sucharek

      It didn't output anything

      M Offline
      M Offline
      mchinand
      wrote on last edited by
      #10

      @Sucharek

      What is the output of
      qDebug() << "Is Settings Writeable? " << settings.isWritable();

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sucharek
        wrote on last edited by
        #11

        Ok, here's the output:
        f7d2302d-f00d-4ab6-acbb-7770c403c48f-image.png
        I've also added:

        qDebug() << "Settings status: " << settings.status();
        

        Which is the "Settings status: ".

        JonBJ 1 Reply Last reply
        0
        • S Sucharek

          Ok, here's the output:
          f7d2302d-f00d-4ab6-acbb-7770c403c48f-image.png
          I've also added:

          qDebug() << "Settings status: " << settings.status();
          

          Which is the "Settings status: ".

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #12

          @Sucharek
          So it isn't writeable. Which we already know, because resource files are not writeable. You cannot set settings in a resource file. That's the answer.

          Hi, I want to save some variable values into a config file.

          I tried doing it with QSettings and resources, but it doesn't seem to work.

          Yes, it does not work.

          1 Reply Last reply
          3
          • S Offline
            S Offline
            Sucharek
            wrote on last edited by
            #13

            Ok, so , can I use like a full path? (C:/...)

            M 1 Reply Last reply
            0
            • S Sucharek

              Ok, so , can I use like a full path? (C:/...)

              M Offline
              M Offline
              mchinand
              wrote on last edited by
              #14

              No, resource files don't have a location on disk that you can specify other than how you have above (:/...), they are stored embedded in the executable.

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

                Hi,

                If you properly configure your QApplication, QSettings will by default store your settings in the appropriate location for the OS your application runs on.

                If you really want to have a file, then use QStandardPaths to query a suitable folder.

                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
                1
                • S Offline
                  S Offline
                  Sucharek
                  wrote on last edited by
                  #16

                  Ok, so how do I store it in the :/config.ini now?

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

                    As was already explained several times by my fellows on this thread: you cannot write in the resources.

                    These are read-only data embedded in your executable.

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

                    S 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      As was already explained several times by my fellows on this thread: you cannot write in the resources.

                      These are read-only data embedded in your executable.

                      S Offline
                      S Offline
                      Sucharek
                      wrote on last edited by
                      #18

                      Oh, ok so now can I use the QStandardPaths? And if so, how?

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

                        Where do you want to store your configuration ?

                        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
                        0
                        • S Offline
                          S Offline
                          Sucharek
                          wrote on last edited by
                          #20

                          Well, I'm building the app for an Android device, so I would put it to: /data/data/org.qtproject.example.clicker/
                          if reading/writing to that folder isn't possible (without root), I would put it to:
                          /sdcard/Android/data/org.qtproject.example.clicker/

                          J.HilkJ 1 Reply Last reply
                          0
                          • S Sucharek

                            Well, I'm building the app for an Android device, so I would put it to: /data/data/org.qtproject.example.clicker/
                            if reading/writing to that folder isn't possible (without root), I would put it to:
                            /sdcard/Android/data/org.qtproject.example.clicker/

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #21

                            @Sucharek

                            I actually have a ini file that I ship in one of my applications with the resource system. On startup I check, if my ini file exists in the relating appdata system.
                            If not I copy it from the resources.

                            This is made for Windows, but since its Qt, it should work just as well for Android.

                            Here are the bare bone functions:

                            //From to create class PreAppTasks:
                            void PreAppTasks::copyIniToDestDir()
                            {
                                QFile *destFile = getDestIniFile();
                                //QFile *orgFile  = getOrgIniFile();
                                //in your case simply:
                                QFile *orgFile = new File(":/config/config.ini");
                            
                                if(orgFile){
                                    if(destFile && destFile->exists()){
                                        //Both Files exists: implement your logic, for this case here
                                    } else {
                                        //DestFile does not exist (propably the first program execution) -> simply copy from install folder
                                        //But first, create the folder, if it does not exist
                                        QFileInfo info(destFile->fileName());
                                        info.setFile(info.absolutePath());
                                        if(!info.exists()){
                                            QDir d;
                                            d.mkpath(info.absoluteFilePath());
                                        }
                                        orgFile->copy(destFile->fileName());
                                    }
                                }
                            
                                if(destFile){
                                    destFile->deleteLater();
                                    destFile = nullptr;
                                }
                                if(orgFile){
                                    orgFile->deleteLater();
                                    orgFile = nullptr;
                                }
                            }
                            
                            
                            QFile *PreAppTasks::getDestIniFile()
                            {
                            //in your case:
                                return new QFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QString("/config/config.ini"));
                            }
                            
                            //In main.cpp (AFTER application instance creation!)
                            int main(int argc, char *argv[])
                            {
                                QGuiApplication a(argc, argv);
                                PreAppTasks::copyIniToDestDir();
                            ....
                                return a.exec();
                            }
                            

                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            S 1 Reply Last reply
                            6
                            • J.HilkJ J.Hilk

                              @Sucharek

                              I actually have a ini file that I ship in one of my applications with the resource system. On startup I check, if my ini file exists in the relating appdata system.
                              If not I copy it from the resources.

                              This is made for Windows, but since its Qt, it should work just as well for Android.

                              Here are the bare bone functions:

                              //From to create class PreAppTasks:
                              void PreAppTasks::copyIniToDestDir()
                              {
                                  QFile *destFile = getDestIniFile();
                                  //QFile *orgFile  = getOrgIniFile();
                                  //in your case simply:
                                  QFile *orgFile = new File(":/config/config.ini");
                              
                                  if(orgFile){
                                      if(destFile && destFile->exists()){
                                          //Both Files exists: implement your logic, for this case here
                                      } else {
                                          //DestFile does not exist (propably the first program execution) -> simply copy from install folder
                                          //But first, create the folder, if it does not exist
                                          QFileInfo info(destFile->fileName());
                                          info.setFile(info.absolutePath());
                                          if(!info.exists()){
                                              QDir d;
                                              d.mkpath(info.absoluteFilePath());
                                          }
                                          orgFile->copy(destFile->fileName());
                                      }
                                  }
                              
                                  if(destFile){
                                      destFile->deleteLater();
                                      destFile = nullptr;
                                  }
                                  if(orgFile){
                                      orgFile->deleteLater();
                                      orgFile = nullptr;
                                  }
                              }
                              
                              
                              QFile *PreAppTasks::getDestIniFile()
                              {
                              //in your case:
                                  return new QFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QString("/config/config.ini"));
                              }
                              
                              //In main.cpp (AFTER application instance creation!)
                              int main(int argc, char *argv[])
                              {
                                  QGuiApplication a(argc, argv);
                                  PreAppTasks::copyIniToDestDir();
                              ....
                                  return a.exec();
                              }
                              
                              S Offline
                              S Offline
                              Sucharek
                              wrote on last edited by
                              #22

                              @J-Hilk Thanks for the reply.
                              I'm not home right now, so I'll try it later.
                              Then I'll inform you if it works.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                Sucharek
                                wrote on last edited by
                                #23

                                Hi, so I tried to add it to my code, but I don't know where.
                                Could you please tell me how to do it, or do I need to create a new C++ Class?

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

                                  @J-Hilk said in Save values to a config file:

                                  //In main.cpp (AFTER application instance creation!)
                                  int main(int argc, char *argv[])
                                  {
                                  QGuiApplication a(argc, argv);
                                  PreAppTasks::copyIniToDestDir();
                                  ....
                                  return a.exec();
                                  }

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

                                  S 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    @J-Hilk said in Save values to a config file:

                                    //In main.cpp (AFTER application instance creation!)
                                    int main(int argc, char *argv[])
                                    {
                                    QGuiApplication a(argc, argv);
                                    PreAppTasks::copyIniToDestDir();
                                    ....
                                    return a.exec();
                                    }

                                    S Offline
                                    S Offline
                                    Sucharek
                                    wrote on last edited by
                                    #25

                                    @SGaist Yes, I've seen that, but I want to know where to put the code before.

                                    //From to create class PreAppTasks:
                                    void PreAppTasks::copyIniToDestDir()
                                    {
                                        QFile *destFile = getDestIniFile();
                                        //QFile *orgFile  = getOrgIniFile();
                                        //in your case simply:
                                        QFile *orgFile = new File(":/config/config.ini");
                                    
                                        if(orgFile){
                                            if(destFile && destFile->exists()){
                                                //Both Files exists: implement your logic, for this case here
                                            } else {
                                                //DestFile does not exist (propably the first program execution) -> simply copy from install folder
                                                //But first, create the folder, if it does not exist
                                                QFileInfo info(destFile->fileName());
                                                info.setFile(info.absolutePath());
                                                if(!info.exists()){
                                                    QDir d;
                                                    d.mkpath(info.absoluteFilePath());
                                                }
                                                orgFile->copy(destFile->fileName());
                                            }
                                        }
                                    
                                        if(destFile){
                                            destFile->deleteLater();
                                            destFile = nullptr;
                                        }
                                        if(orgFile){
                                            orgFile->deleteLater();
                                            orgFile = nullptr;
                                        }
                                    }
                                    
                                    
                                    QFile *PreAppTasks::getDestIniFile()
                                    {
                                    //in your case:
                                        return new QFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QString("/config/config.ini"));
                                    }
                                    

                                    That code

                                    J.HilkJ 1 Reply Last reply
                                    0
                                    • S Sucharek

                                      @SGaist Yes, I've seen that, but I want to know where to put the code before.

                                      //From to create class PreAppTasks:
                                      void PreAppTasks::copyIniToDestDir()
                                      {
                                          QFile *destFile = getDestIniFile();
                                          //QFile *orgFile  = getOrgIniFile();
                                          //in your case simply:
                                          QFile *orgFile = new File(":/config/config.ini");
                                      
                                          if(orgFile){
                                              if(destFile && destFile->exists()){
                                                  //Both Files exists: implement your logic, for this case here
                                              } else {
                                                  //DestFile does not exist (propably the first program execution) -> simply copy from install folder
                                                  //But first, create the folder, if it does not exist
                                                  QFileInfo info(destFile->fileName());
                                                  info.setFile(info.absolutePath());
                                                  if(!info.exists()){
                                                      QDir d;
                                                      d.mkpath(info.absoluteFilePath());
                                                  }
                                                  orgFile->copy(destFile->fileName());
                                              }
                                          }
                                      
                                          if(destFile){
                                              destFile->deleteLater();
                                              destFile = nullptr;
                                          }
                                          if(orgFile){
                                              orgFile->deleteLater();
                                              orgFile = nullptr;
                                          }
                                      }
                                      
                                      
                                      QFile *PreAppTasks::getDestIniFile()
                                      {
                                      //in your case:
                                          return new QFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QString("/config/config.ini"));
                                      }
                                      

                                      That code

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #26

                                      @Sucharek said in Save values to a config file:

                                      Yes, I've seen that, but I want to know where to put the code before.

                                      //From to create class PreAppTasks:

                                      -> Functions/Code taken from a class that you yourself will have to create. In this case I originally named it PreAppTasks. That is why you will find the class Prefix PreAppTasks before the the function bodies. You can, but you don't have to use the same class name, that is up to your discretion.

                                      ...
                                      Was a bit long for a comment so I shortened it.


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      S 1 Reply Last reply
                                      0
                                      • J.HilkJ J.Hilk

                                        @Sucharek said in Save values to a config file:

                                        Yes, I've seen that, but I want to know where to put the code before.

                                        //From to create class PreAppTasks:

                                        -> Functions/Code taken from a class that you yourself will have to create. In this case I originally named it PreAppTasks. That is why you will find the class Prefix PreAppTasks before the the function bodies. You can, but you don't have to use the same class name, that is up to your discretion.

                                        ...
                                        Was a bit long for a comment so I shortened it.

                                        S Offline
                                        S Offline
                                        Sucharek
                                        wrote on last edited by J.Hilk
                                        #27

                                        @J-Hilk Ok, so I created a new class (PreAppTasks) and put in the code, but I have some errors:

                                        out-of-line definition of 'copyIniToDestDir' does not match any declaration in 'PreAppTasks'
                                        use of undeclared identifier 'getDestIniFile'
                                        unknown type name 'File'
                                        out-of-line definition of 'getDestIniFile' does not match any declaration in 'PreAppTasks'

                                        my preapptasks.cpp

                                        #include "preapptasks.h"
                                        #include <QFile>
                                        #include <QFileInfo>
                                        #include <QDir>
                                        #include <QStandardPaths>
                                        
                                        PreAppTasks::PreAppTasks()
                                        {
                                        
                                        }
                                        
                                        void PreAppTasks::copyIniToDestDir()
                                        {
                                            QFile *destFile = getDestIniFile();
                                            //QFile *orgFile  = getOrgIniFile();
                                            //in your case simply:
                                            QFile *orgFile = new QFile(":/config/config.ini");
                                        
                                            if(orgFile){
                                                if(destFile && destFile->exists()){
                                                    //Both Files exists: implement your logic, for this case here
                                                } else {
                                                    //DestFile does not exist (propably the first program execution) -> simply copy from install folder
                                                    //But first, create the folder, if it does not exist
                                                    QFileInfo info(destFile->fileName());
                                                    info.setFile(info.absolutePath());
                                                    if(!info.exists()){
                                                        QDir d;
                                                        d.mkpath(info.absoluteFilePath());
                                                    }
                                                    orgFile->copy(destFile->fileName());
                                                }
                                            }
                                        
                                            if(destFile){
                                                destFile->deleteLater();
                                                destFile = nullptr;
                                            }
                                            if(orgFile){
                                                orgFile->deleteLater();
                                                orgFile = nullptr;
                                            }
                                        }
                                        
                                        
                                        QFile *PreAppTasks::getDestIniFile()
                                        {
                                        //in your case:
                                            return new QFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QString("/config/config.ini"));
                                        }
                                        
                                        
                                        JonBJ 1 Reply Last reply
                                        0
                                        • S Sucharek

                                          @J-Hilk Ok, so I created a new class (PreAppTasks) and put in the code, but I have some errors:

                                          out-of-line definition of 'copyIniToDestDir' does not match any declaration in 'PreAppTasks'
                                          use of undeclared identifier 'getDestIniFile'
                                          unknown type name 'File'
                                          out-of-line definition of 'getDestIniFile' does not match any declaration in 'PreAppTasks'

                                          my preapptasks.cpp

                                          #include "preapptasks.h"
                                          #include <QFile>
                                          #include <QFileInfo>
                                          #include <QDir>
                                          #include <QStandardPaths>
                                          
                                          PreAppTasks::PreAppTasks()
                                          {
                                          
                                          }
                                          
                                          void PreAppTasks::copyIniToDestDir()
                                          {
                                              QFile *destFile = getDestIniFile();
                                              //QFile *orgFile  = getOrgIniFile();
                                              //in your case simply:
                                              QFile *orgFile = new QFile(":/config/config.ini");
                                          
                                              if(orgFile){
                                                  if(destFile && destFile->exists()){
                                                      //Both Files exists: implement your logic, for this case here
                                                  } else {
                                                      //DestFile does not exist (propably the first program execution) -> simply copy from install folder
                                                      //But first, create the folder, if it does not exist
                                                      QFileInfo info(destFile->fileName());
                                                      info.setFile(info.absolutePath());
                                                      if(!info.exists()){
                                                          QDir d;
                                                          d.mkpath(info.absoluteFilePath());
                                                      }
                                                      orgFile->copy(destFile->fileName());
                                                  }
                                              }
                                          
                                              if(destFile){
                                                  destFile->deleteLater();
                                                  destFile = nullptr;
                                              }
                                              if(orgFile){
                                                  orgFile->deleteLater();
                                                  orgFile = nullptr;
                                              }
                                          }
                                          
                                          
                                          QFile *PreAppTasks::getDestIniFile()
                                          {
                                          //in your case:
                                              return new QFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QString("/config/config.ini"));
                                          }
                                          
                                          
                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by
                                          #28

                                          @Sucharek said in Save values to a config file:

                                          my preapptasks.cpp

                                          So how does this compare to what is in your preapptasks.h?

                                          S 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