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
-
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? -
@Qt-Enthusiast
See this: http://doc.qt.io/qt-5/qsettings.html
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
http://doc.qt.io/qt-5/qsettings.html#registerFormat -
@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
-
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 thesettings->value(yourkey)
correctly in the code ;)--
edit: example addedi 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; }
-
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 savesiif (userSpecifiedConf)
QSettings settings (userConf, QSettings::IniFormat);
else
QSettings settings;Please let me know if this solution is OK
-
@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