PDA

View Full Version : Why QDir::path() doesn't return a trailing slash, and backslashes in QSettings



johnkent222
19th April 2010, 15:59
In Qt 4.6, I'm curious if anyone knows the reason why the string returned from QDir::path() doesn't end with a "/".

Also, I'm curious if anyone knows why QSettings does not tolerate backslashes in Windows configuration files. It appears that backslashes are read as escape characters. Why would anyone put escape characters in a configuration file?

Thank you for your help.

wysota
19th April 2010, 16:07
In Qt 4.6, I'm curious if anyone knows the reason why the string returned from QDir::path() doesn't end with a "/".
Why would it end with a slash?

Lykurg
19th April 2010, 16:09
In Qt 4.6, I'm curious if anyone knows the reason why the string returned from QDir::path() doesn't end with a "/".
Because the Trolls decided to do so... And because normally a path does not end with a slash since it is superfluous.
But since you noticed that, you won't get into trouble and you can just concat QDir::separator().

Also, I'm curious if anyone knows why QSettings does not tolerate backslashes in Windows configuration files. It appears that backslashes are read as escape characters. Why would anyone put escape characters in a configuration file?Because the slash is "standard" and windows abused the escape character. But you can use "\\"...

johnkent222
19th April 2010, 22:45
Thank you Lykurg. I figured it was a philosophical decision for them.

I'm still confused as to why backslashes in configuration files would be treated as escape characters. It's not like anyone would put a "\t" in their config file instead of an actual tab (or would they?)

wysota
19th April 2010, 22:49
If you mean that you can't do:

QSettings settings(...);
settings.setValue("some\thing", 7);
...then QSettings has nothing to do with that. It's the compiler that treats the backslash as an escape character. If you did:

QSettings settings(...);
settings.setValue("some\\thing", 7);
...it should work correctly.

johnkent222
22nd April 2010, 16:37
What happens is that if one of the parameters in an INI file (or any configuration file) is a windows path, a parameter like

directory=C:\Directory\Path\

when processed with QSettings, will return "C:irecotryath", treating "\D" and "\P" as escaped characters. Even in the documentation, it is stated that a Windows path in a INI settings file is not supported because they use backslashes, and backslashes are intended to escape characters.

However, like you said, we use backslashes to escape characters in code, not in a plain text INI settings file. It seems odd that backslashes in an INI file would be treated as escape characters. The code in QSettings goes to great lengths to parse out backslashes and treat them as escape characters, so there must be a reason for it.

Does anyone know what that reason is?

wysota
22nd April 2010, 16:43
Wrap the value into quotes.

directory="C:\Directory\Path\"