Take the 2-minute tour ×

I have a program that gives a QString and changes every "\" to "/". It seems very simple but when I use the below code, 5 errors happen:

QString path ;
path = "C:\MyLife\Image Collection" ;
    if( path[i] == "\" )
        path[i] = "/" ;
qDebug() << path ;
share|improve this question
"5 errors happen..." Please provide detail about those errors. –  JYelton Mar 26 '11 at 9:40
For one thing " denotes a string in C/C++ - use ' for characters. –  therefromhere Mar 26 '11 at 9:56
you should have a look at QString::replace and QDir::toNativeSeparators –  Frank Osterfeld Mar 26 '11 at 13:07
Welcome to StackOverflow. Please review the faq. It has come to our attention that you have provided few answers, have accepted no answers, and have not thanked users who have helped you by upvoting their answers. You also do not pay it forward by occasionally trying to help other community members get answers to their questions, too. If this behavior becomes a pattern, your account may be suspended. Please realize people are trying to help you, and be considerate of their efforts. –  Will Apr 4 '11 at 14:35
Just get in the habit of using / in paths for Qt, it works on every platform, the `` is just windows I think, MS and their incompatibility urges... –  ddriver Jul 5 '13 at 13:32
show 2 more comments

6 Answers

Please, Stop the bleeding, now ! And use a cross-platform directory/path wrapper class. Qt have some : QDir, QFileInfo, QFile. Just use them.

ooh, and QDir have a nice static method for you, which does exactly what you want :

 path = QDir::fromNativeSeparators(path);

No excuse to do it manually (with bugs)

share|improve this answer
Does not work on Linux! –  abergmeier Jul 5 '13 at 13:19
add comment

You need to escape \

if( path[i] == '\\' )

Same with

path = "C:\\MyLife\\Image Collection" ;


share|improve this answer
Or use C++0x raw strings! (no escaping required) –  David Mar 26 '11 at 10:32
add comment

Because the backslash \ is used as an escape character (for things like \n newline, \r carriage return, and \b backspace), you need to escape the backslash with another backslash to give you a literal backslash. That is, wherever you want a \, you put \\.

share|improve this answer
add comment
  • Strings cannot be compared directly in C/C++.
  • Characters can be compared.
  • "\" is string, whereas '\' is a character.
share|improve this answer
You mean: "\\" is a string, whereas '\\' is a character. –  TonyK Mar 26 '11 at 10:14
@tonyk yep thanks. –  N 1.1 Mar 26 '11 at 10:21
add comment

Nobody has fixed both your errors in the same post, so here goes:

    if( path[i] == '\\' ) // Double backslash required, and
        path[i] = '/' ;   // single quote (both times!)
share|improve this answer
add comment

What does work for me for Qt4 on Linux is using:

std::replace( path.begin(), path.end(), QChar('\\'), QChar('/') );

None of the Qt functions work, obviously.

share|improve this answer
add comment

Your Answer


