Important: Please read the Qt Code of Conduct -

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

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

  • @Qt-Enthusiast

    QSettings settings("/home/user/conf.conf", QSettings::IniFormat);

    Then you can get values like this:

    QString value1 = settings.value("Key1").toString();

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

  • @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.

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

  • @Qt-Enthusiast
    See this:
    I don't think there is default path with "QSettings::IniFormat".

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

  • @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               md5
    # IPv6 local connections:
    host    all             all             ::1/128                 md5

  • @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(";");
            return true;
        foreach(QString k,localSplit) {
            QStringList split = k.split(("=>"));
        return true;
    bool write(QIODevice &dev,const QSettings::SettingsMap &map) {
        qDebug() << "SETTINGS WRITER";
        QString s;
        foreach(QString k,map.keys())
        s = s.remove(s.length()-1,1);
        SimpleCrypt cryptokey;
        cryptokey.setKey(/*secret ;) */);
        QByteArray enctxt = cryptokey.encryptToByteArray(s);
        return true;

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

  • 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);
    QSettings settings;

    Please let me know if this solution is OK

  • Lifetime Qt Champion

    @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

Log in to reply