PDA

View Full Version : QSettings doesn't return a text with backslashs inside quotes correctly



nilot
12th October 2016, 11:51
Hello,

Let say that I have the following ini file :

[General]
path="C:\Directory"

But with :

QSettings settings(_configFilePath, QSettings::IniFormat);
qDebug()<<settings.value("path");

I get this result which is wrong : QVariant(QString, "C:irectory"). How can I get the correct path ?

Thank you

Lesiok
12th October 2016, 12:38
Read QSettings doc. This problem is well described in.

nilot
12th October 2016, 12:50
Ok but I asked the question because according to the last answer of this thread : http://www.qtcentre.org/threads/29996-Why-QDir-path()-doesn-t-return-a-trailing-slash-and-backslashes-in-QSettings it is possible .

Lesiok
12th October 2016, 13:28
This thread is 6 years old. Something changed in Qt ? Generally backslash is an 'escape character'.
Is this INI file used by another software ? Just use a slash ( / ) as path separator - it is working well on all Qt platforms.

nilot
12th October 2016, 13:39
In the ini file, users can change the path of an image used by my software. As I am working on windows, the file's path contains backslashes. So the only solution I have is to force users to write the path with normal slashes ?

Lesiok
12th October 2016, 15:09
First slash is legal path separator on Windows.
Second I do not think manual tinkering with INI file by the user was a good idea.

jefftee
13th October 2016, 01:06
The backslash in a C++ string being an escape character has nothing to do with Qt of course. Just use a forward slash in your string literals as described in the docs, Qt does the translation to the native OS preference behind the scenes.