error read with useing QSettings
-
I use follow code to read ini file:
QSettings settings(fileName, QSettings::IniFormat); settings.setIniCodec(QTextCodec::codecForName("utf-8")); settings.value("/Group/name", "default").toString();
the value in the file without comma is right, but it goes error when it contains , and the strange is if comma is under Chinese input method not English it is right. How to solve this problem?
-
@MLGDN Hi,friend. Welcome.
I think you should not set
utf-8
. In Qt5 is not to set, just like below:QSettings settings("test.ini", QSettings::IniFormat); //settings.setIniCodec(QTextCodec::codecForName("utf-8")); settings.setValue("key1","IamEnglish"); settings.setValue("key2","I,am,English"); settings.setValue("key3","我,是,中文"); qDebug() << settings.value("key1","Default").toString(); qDebug() << settings.value("key2","Default").toString(); qDebug() << settings.value("key3","Default").toString();
and the ini file:
[General] key1=IamEnglish key2="I,am,English" key3=\x6211\xff0c\x662f\xff0c\x4e2d\x6587
In the ini file, Qt used
\x
to encode Chinese. -
@MLGDN
When there is a comma in value of ini file and if you read the value with a key, it will return a list.
settings.value("/Group/name", "default").toStringList();
will return an array of strings.
for eg. if you havetestKey=a,b,c,d
in ini file and if you fetch valueqDebug() << settings.value("testKey","default").toStringList();
you will get
("a", "b", "c", "d")
-
@Tirupathi-Korla
thank you , it works