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
Forum Updated to NodeBB v4.3 + New Features

Save values to a config file

Scheduled Pinned Locked Moved Solved General and Desktop
43 Posts 7 Posters 7.6k Views 4 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.
  • 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 Online
          J.HilkJ Online
          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
              • JonBJ JonB

                @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 Offline
                S Offline
                Sucharek
                wrote on last edited by
                #29

                @JonB Well, I didn't do anything with preapptasks.h.
                What do I add in there?

                JonBJ 1 Reply Last reply
                0
                • S Sucharek

                  @JonB Well, I didn't do anything with preapptasks.h.
                  What do I add in there?

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

                  @Sucharek
                  @J.Hilk gave you the definitions of a couple of methods of a PreAppTasks class. So in C++ you know you will need to provide the necessary class declaration in the .h file. Which I think from your error messages you have not done.

                  S 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Sucharek
                    @J.Hilk gave you the definitions of a couple of methods of a PreAppTasks class. So in C++ you know you will need to provide the necessary class declaration in the .h file. Which I think from your error messages you have not done.

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

                    @JonB Ok, so I added
                    copyIniToDestDir();
                    getDestIniFile();
                    to the .h file, but I'm not sure what to declare there (err message: C++ requires a type specifier for all declarations).
                    I'm also having a problem with declaring the File variable.
                    On
                    QFile *orgFile = new File(":/config/config.ini");
                    when I try to declare it in the .h file (QFile File;), it doesn't fix the error message (unknown type name 'File').

                    JonBJ 1 Reply Last reply
                    0
                    • S Sucharek

                      @JonB Ok, so I added
                      copyIniToDestDir();
                      getDestIniFile();
                      to the .h file, but I'm not sure what to declare there (err message: C++ requires a type specifier for all declarations).
                      I'm also having a problem with declaring the File variable.
                      On
                      QFile *orgFile = new File(":/config/config.ini");
                      when I try to declare it in the .h file (QFile File;), it doesn't fix the error message (unknown type name 'File').

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

                      @Sucharek
                      At this point you need to learn some basic C++ if you expect to code with Qt. You cannot do so without knowing the language,

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

                        Hi, so I've looked into it, and fixed almost all errors, except the "unknown type name 'File'".
                        I still can't figure out how to fix that one.

                        J.HilkJ 1 Reply Last reply
                        0
                        • S Sucharek

                          Hi, so I've looked into it, and fixed almost all errors, except the "unknown type name 'File'".
                          I still can't figure out how to fix that one.

                          J.HilkJ Online
                          J.HilkJ Online
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #34

                          @Sucharek thats actually a typo, and I now see, thats actually from my original code, I blame auto correction:

                          QFile *orgFile = new File(":/config/config.ini");
                          QFile *orgFile = new QFile(":/config/config.ini");


                          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.

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

                            Hi, ok, when I get home, I'll try it.

                            JonBJ 1 Reply Last reply
                            0
                            • S Sucharek

                              Hi, ok, when I get home, I'll try it.

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

                              @Sucharek
                              I don't know whether the other C++ experts would concur (comments always welcome!), but if you are intending to put

                              QFile *orgFile = new QFile(":/config/config.ini");
                              

                              in the class member variables declaration in the .h file, I wouldn't. I would suggest such an initialisation (i.e. the new ... part) belongs better in the class constructor.

                              S 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @Sucharek
                                I don't know whether the other C++ experts would concur (comments always welcome!), but if you are intending to put

                                QFile *orgFile = new QFile(":/config/config.ini");
                                

                                in the class member variables declaration in the .h file, I wouldn't. I would suggest such an initialisation (i.e. the new ... part) belongs better in the class constructor.

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

                                @JonB
                                As long as it works, I'm good with it.

                                I also find interesting that in this part of the code:

                                if(orgFile){
                                        if(destFile && destFile->exists()){
                                            qDebug() << "exists";
                                        } else {
                                

                                If i change the resource prefix, or the config.ini name, it will STILL print "exists".
                                I also have another question. How do I write and read the file, now that the code probably works?

                                artwawA 1 Reply Last reply
                                0
                                • S Sucharek

                                  @JonB
                                  As long as it works, I'm good with it.

                                  I also find interesting that in this part of the code:

                                  if(orgFile){
                                          if(destFile && destFile->exists()){
                                              qDebug() << "exists";
                                          } else {
                                  

                                  If i change the resource prefix, or the config.ini name, it will STILL print "exists".
                                  I also have another question. How do I write and read the file, now that the code probably works?

                                  artwawA Offline
                                  artwawA Offline
                                  artwaw
                                  wrote on last edited by
                                  #38

                                  @Sucharek https://doc.qt.io/qt-5/qfile.html is what you need.

                                  Please learn at least the absolute basics of C++ (and Qt while we're at it - both are very well documented), if your intention is to make it work by using the code snippets provided by the others you'll get nowhere in the long run.

                                  For more information please re-read.

                                  Kind Regards,
                                  Artur

                                  S 1 Reply Last reply
                                  0
                                  • artwawA artwaw

                                    @Sucharek https://doc.qt.io/qt-5/qfile.html is what you need.

                                    Please learn at least the absolute basics of C++ (and Qt while we're at it - both are very well documented), if your intention is to make it work by using the code snippets provided by the others you'll get nowhere in the long run.

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

                                    Hi, so I've looked into it and I can read from the file, but can't write.
                                    I've tried multiple methods, but they don't seem to work.
                                    my .cpp file:

                                    #include "preapptasks.h"
                                    QFile *orgFile;
                                    
                                    PreAppTasks::PreAppTasks()
                                    {
                                    
                                    }
                                    
                                    QFile *PreAppTasks::getDestIniFile()
                                    {
                                    //in your case:
                                        return new QFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QString("/config/config.ini"));
                                    }
                                    
                                    void PreAppTasks::copyIniToDestDir()
                                    {
                                        orgFile = new QFile(":/config/config.ini");
                                        QTextStream in(orgFile);
                                        QFile *destFile = getDestIniFile();
                                        //QFile *orgFile  = getOrgIniFile();
                                        //in your case simply:
                                    
                                        if(orgFile){
                                            if(destFile && destFile->exists()){
                                                    orgFile->open(QIODevice::ReadOnly | QIODevice::Text);
                                                    qDebug() << orgFile->isOpen();
                                                    QString line = in.readLine();
                                                    qDebug() << line;
                                                    orgFile->close();
                                    
                                                    orgFile->open(stdin, QIODevice::ReadWrite | QIODevice::Text);
                                                    qDebug() << orgFile->isOpen();
                                                    orgFile->write("a");
                                                    line = in.readLine();
                                                    qDebug() << line;
                                                    orgFile->close();
                                            } 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;
                                        }
                                    }
                                    

                                    So, in
                                    if(destFile && destFile->exists()){ ... }
                                    I tried to read the contents of the file (currently 50), which worked, but writing doesn't.
                                    EDIT: Also, when I try to read the line on ReadOnly mode, it works, but in ReadWrite mode, it doesn't even print anything.

                                    P. S.: I'm sorry that you have to deal with me, but I just want to get this to work.

                                    J.HilkJ 1 Reply Last reply
                                    0
                                    • S Sucharek

                                      Hi, so I've looked into it and I can read from the file, but can't write.
                                      I've tried multiple methods, but they don't seem to work.
                                      my .cpp file:

                                      #include "preapptasks.h"
                                      QFile *orgFile;
                                      
                                      PreAppTasks::PreAppTasks()
                                      {
                                      
                                      }
                                      
                                      QFile *PreAppTasks::getDestIniFile()
                                      {
                                      //in your case:
                                          return new QFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QString("/config/config.ini"));
                                      }
                                      
                                      void PreAppTasks::copyIniToDestDir()
                                      {
                                          orgFile = new QFile(":/config/config.ini");
                                          QTextStream in(orgFile);
                                          QFile *destFile = getDestIniFile();
                                          //QFile *orgFile  = getOrgIniFile();
                                          //in your case simply:
                                      
                                          if(orgFile){
                                              if(destFile && destFile->exists()){
                                                      orgFile->open(QIODevice::ReadOnly | QIODevice::Text);
                                                      qDebug() << orgFile->isOpen();
                                                      QString line = in.readLine();
                                                      qDebug() << line;
                                                      orgFile->close();
                                      
                                                      orgFile->open(stdin, QIODevice::ReadWrite | QIODevice::Text);
                                                      qDebug() << orgFile->isOpen();
                                                      orgFile->write("a");
                                                      line = in.readLine();
                                                      qDebug() << line;
                                                      orgFile->close();
                                              } 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;
                                          }
                                      }
                                      

                                      So, in
                                      if(destFile && destFile->exists()){ ... }
                                      I tried to read the contents of the file (currently 50), which worked, but writing doesn't.
                                      EDIT: Also, when I try to read the line on ReadOnly mode, it works, but in ReadWrite mode, it doesn't even print anything.

                                      P. S.: I'm sorry that you have to deal with me, but I just want to get this to work.

                                      J.HilkJ Online
                                      J.HilkJ Online
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #40

                                      @Sucharek said in Save values to a config file:

                                      I'm sorry that you have to deal with me, but I just want to get this to work.

                                      Don't worry, we all started somewhere sometime.

                                      And I brought this path up, so I'll see it finished :P

                                      Give me a some time, I'll make an working example


                                      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:

                                        I'm sorry that you have to deal with me, but I just want to get this to work.

                                        Don't worry, we all started somewhere sometime.

                                        And I brought this path up, so I'll see it finished :P

                                        Give me a some time, I'll make an working example

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

                                        @J-Hilk Thanks for the confidence boost :)

                                        J.HilkJ 1 Reply Last reply
                                        0
                                        • S Sucharek

                                          @J-Hilk Thanks for the confidence boost :)

                                          J.HilkJ Online
                                          J.HilkJ Online
                                          J.Hilk
                                          Moderators
                                          wrote on last edited by
                                          #42

                                          @Sucharek
                                          here ya go:

                                          https://github.com/DeiVadder/PersistentConfigIni.git

                                          Interestingly enough, I found a bug in my old code.
                                          It wasn't necessary that I changed the config file so I never noticed. But one has to change the permission of the copied file to also be writeable. Since it's copied from a read only directory and that property carries over.

                                          🤷‍♂️


                                          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
                                          2

                                          • Login

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