Results 1 to 3 of 3

Thread: “/” And “\” problem

  1. #1
    Join Date
    Mar 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default “/” And “\” problem

    Hi I have a program that do this : gives a QString and change every "\" with "/" . It seems very simple but when I write below code 5 errors happen . please help me . thanks.

    My code :

    Qt Code:
    1. QString path ;
    2. path = "C:\MyLife\Image Collection" ;
    3. for( int i=0 ; i < path.size() ; i++ )
    4. {
    5. if( path[i] == "\" )
    6. path[i] = "/" ;
    7. }
    8. qDebug() << path ;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: “/” And “\” problem

    It depends; what are the errors?

    I see one problem; on lines 4 and 5, you're treating the individual characters as strings rather than characters. Try replacing the double quotes with single quotes around the slash characters.

    Also, since you're using a QString, consider using QString.replace('\','/') and eliminating the loop and test altogether.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: “/” And “\” problem

    '\' is escape char, you need to double it if you want to use it as an actual char.

    But if this is for changing slashes in directory names, this method already exists in Qt library: http://doc.qt.nokia.com/stable/qdir.html

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.