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. f I have conf file , how to read those settings in QSettings
Forum Updated to NodeBB v4.3 + New Features

f I have conf file , how to read those settings in QSettings

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 7.0k 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.
  • Q Qt Enthusiast

    I have config file /home/user/conf.conf is there any way I read the settings in QSetting from thos conf.conf file

    the_T Offline
    the_T Offline
    the_
    wrote on last edited by
    #3

    @Qt-Enthusiast
    what's the format of the stored settings in the conf file? can you show an example file?

    -- No support in PM --

    S 1 Reply Last reply
    1
    • the_T the_

      @Qt-Enthusiast
      what's the format of the stored settings in the conf file? can you show an example file?

      S Offline
      S Offline
      Stoyan
      wrote on last edited by Stoyan
      #4

      @the_
      On Linux in most of the cases conf-file is the same format as ini-file:

      key = value
      

      If in this case it is not, then he can't use QSettings at all and have to read and parse the file manually.

      Q the_T 2 Replies Last reply
      0
      • S Stoyan

        @the_
        On Linux in most of the cases conf-file is the same format as ini-file:

        key = value
        

        If in this case it is not, then he can't use QSettings at all and have to read and parse the file manually.

        Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #5

        @Stoyan
        It is same as where default QSettings are stored in file . By the way what is exact path default QSettings of GUI are save

        S 1 Reply Last reply
        0
        • Q Qt Enthusiast

          @Stoyan
          It is same as where default QSettings are stored in file . By the way what is exact path default QSettings of GUI are save

          S Offline
          S Offline
          Stoyan
          wrote on last edited by Stoyan
          #6

          @Qt-Enthusiast
          See this: http://doc.qt.io/qt-5/qsettings.html
          I don't think there is default path with "QSettings::IniFormat".

          1 Reply Last reply
          0
          • S Stoyan

            @the_
            On Linux in most of the cases conf-file is the same format as ini-file:

            key = value
            

            If in this case it is not, then he can't use QSettings at all and have to read and parse the file manually.

            the_T Offline
            the_T Offline
            the_
            wrote on last edited by the_
            #7

            @Stoyan
            yes I know that there is a 'default' format but who says that all conf file look like this? seen too many of them in the last 15years on Unix systems ;)

            you could still register your own reader and writer functions to use your own format
            http://doc.qt.io/qt-5/qsettings.html#registerFormat

            -- No support in PM --

            S 1 Reply Last reply
            2
            • the_T the_

              @Stoyan
              yes I know that there is a 'default' format but who says that all conf file look like this? seen too many of them in the last 15years on Unix systems ;)

              you could still register your own reader and writer functions to use your own format
              http://doc.qt.io/qt-5/qsettings.html#registerFormat

              S Offline
              S Offline
              Stoyan
              wrote on last edited by
              #8

              @the_
              That's right. There are many formats.
              I never used this function, but I see this:

              The readFunc and writeFunc parameters are pointers to functions that read and write a set of key/value pairs.
              

              I think that this will not work in case of other format without key/value structure like this:

              # TYPE  DATABASE        USER            ADDRESS                 METHOD
              # IPv4 local connections:
              host    all             all             127.0.0.1/32            md5
              # IPv6 local connections:
              host    all             all             ::1/128                 md5
              
              the_T 1 Reply Last reply
              0
              • S Stoyan

                @the_
                That's right. There are many formats.
                I never used this function, but I see this:

                The readFunc and writeFunc parameters are pointers to functions that read and write a set of key/value pairs.
                

                I think that this will not work in case of other format without key/value structure like this:

                # TYPE  DATABASE        USER            ADDRESS                 METHOD
                # IPv4 local connections:
                host    all             all             127.0.0.1/32            md5
                # IPv6 local connections:
                host    all             all             ::1/128                 md5
                
                the_T Offline
                the_T Offline
                the_
                wrote on last edited by the_
                #9

                @Stoyan

                yes you will need a key-value pair, but how you get them inside your functions is your part

                The functions for reading and writing are defined as following

                bool write(QIODevice &dev,const QSettings::SettingsMap &map);
                bool read(QIODevice &dev,QSettings::SettingsMap &map);
                

                you can put everything as key value pairs in map as long as you parse the settings->value(yourkey) correctly in the code ;)

                --
                edit: example added

                i for example have done this for encrypted config file

                bool read(QIODevice &dev,QSettings::SettingsMap &map) {
                    qDebug() << "SETTINGS READER" ;
                    SimpleCrypt cryptokey;
                    cryptokey.setKey(/*secret ;) */);
                
                
                    QByteArray s = dev.readAll();
                
                
                    QString decrypted = cryptokey.decryptToString(s);
                   
                    QStringList localSplit = decrypted.split(";");
                    if(localSplit.length()<2)
                        return true;
                    foreach(QString k,localSplit) {
                        QStringList split = k.split(("=>"));
                        map.insert(split.at(0),split.at(1));
                    }
                
                    return true;
                }
                
                bool write(QIODevice &dev,const QSettings::SettingsMap &map) {
                    qDebug() << "SETTINGS WRITER";
                    QString s;
                    foreach(QString k,map.keys())
                        s.append(k+"=>"+map.value(k).toString()+";");
                
                    s = s.remove(s.length()-1,1);
                  
                    SimpleCrypt cryptokey;
                    cryptokey.setKey(/*secret ;) */);
                
                    QByteArray enctxt = cryptokey.encryptToByteArray(s);
                   
                    dev.write(enctxt,enctxt.length());
                    return true;
                
                }
                

                -- No support in PM --

                S 1 Reply Last reply
                3
                • the_T the_

                  @Stoyan

                  yes you will need a key-value pair, but how you get them inside your functions is your part

                  The functions for reading and writing are defined as following

                  bool write(QIODevice &dev,const QSettings::SettingsMap &map);
                  bool read(QIODevice &dev,QSettings::SettingsMap &map);
                  

                  you can put everything as key value pairs in map as long as you parse the settings->value(yourkey) correctly in the code ;)

                  --
                  edit: example added

                  i for example have done this for encrypted config file

                  bool read(QIODevice &dev,QSettings::SettingsMap &map) {
                      qDebug() << "SETTINGS READER" ;
                      SimpleCrypt cryptokey;
                      cryptokey.setKey(/*secret ;) */);
                  
                  
                      QByteArray s = dev.readAll();
                  
                  
                      QString decrypted = cryptokey.decryptToString(s);
                     
                      QStringList localSplit = decrypted.split(";");
                      if(localSplit.length()<2)
                          return true;
                      foreach(QString k,localSplit) {
                          QStringList split = k.split(("=>"));
                          map.insert(split.at(0),split.at(1));
                      }
                  
                      return true;
                  }
                  
                  bool write(QIODevice &dev,const QSettings::SettingsMap &map) {
                      qDebug() << "SETTINGS WRITER";
                      QString s;
                      foreach(QString k,map.keys())
                          s.append(k+"=>"+map.value(k).toString()+";");
                  
                      s = s.remove(s.length()-1,1);
                    
                      SimpleCrypt cryptokey;
                      cryptokey.setKey(/*secret ;) */);
                  
                      QByteArray enctxt = cryptokey.encryptToByteArray(s);
                     
                      dev.write(enctxt,enctxt.length());
                      return true;
                  
                  }
                  
                  S Offline
                  S Offline
                  Stoyan
                  wrote on last edited by
                  #10

                  @the_
                  Well, now I see that QSettings offer much more possibilities than I suspect. :)

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by
                    #11

                    One solution I have thought
                    Th user in my product will specify the location of my conf and the conf format will be same as that how QSettings saves

                    iif (userSpecifiedConf)
                    QSettings settings (userConf, QSettings::IniFormat);
                    else
                    QSettings settings;

                    Please let me know if this solution is OK

                    jsulmJ 1 Reply Last reply
                    0
                    • Q Qt Enthusiast

                      One solution I have thought
                      Th user in my product will specify the location of my conf and the conf format will be same as that how QSettings saves

                      iif (userSpecifiedConf)
                      QSettings settings (userConf, QSettings::IniFormat);
                      else
                      QSettings settings;

                      Please let me know if this solution is OK

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      @Qt-Enthusiast said in f I have conf file , how to read those settings in QSettings:

                      Th user in my product will specify the location of my conf

                      This is unusual. Usually the config file is located in the home directory of the user as hidden file, like /home/USER_NAME/.myapp
                      This is how UNIX works for decades already, no need to ask the user for that.
                      Global configuration goes to /etc/myapp

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      3

                      • Login

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