Results 1 to 4 of 4

Thread: How to set QFile file name with path with windows separators "\"?

  1. #1
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default How to set QFile file name with path with windows separators "\"?

    Hello,
    When I try to create new file in directory ( that has such permissions ) on Linux it works ok but on Windows I get the errors ( file.errorString() = "The parameter is incorrect" ) , file.error = 5 ):
    Qt Code:
    1. QFile file(outputDir_+"/"+fileName);
    2. if (!file.open(QFile::WriteOnly))
    3. {
    4. QMessageBox::warning(0,"Could not create Project File",
    5. QObject::tr( "\n Could not create Project File on disk"));
    6.  
    7. return false;
    8. }
    To copy to clipboard, switch view to plain text mode 
    In Qt docs it is written that Qt 4.> automatically translates file name with path to such that contains separators for underlying system ( Linux - "/" , Windows - "\" ) and it is enought to pass to QFile constructor path with "/" separators , but when I call file.fileName() on Windows I get the "/" separators, another worlds how to create new file on windows?
    Can anybody help/point what is the problem? Can anybody provide code snippet to create QFile on Windows? What to pass to QFile constructor as file name?
    Thanks in advance.

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to set QFile file name with path with windows separators "\"?

    Your code should work, i.e. (modification from Your previous post, tested on winXP SP3):

    Qt Code:
    1. QString outputDir = QDir::currentPath(); // with "/" separators
    2. QString fileName = QString( "%1%2" ).arg("some_file_name").arg( ".dat" );
    3. QString fileOut = outputDir+ "/" +fileName;
    4.  
    5. QFile file( fileOut );
    6. if (!file.open( QIODevice::WriteOnly )) {
    7. qDebug() << "Could not create Project File";
    8. }else{
    9. qDebug() << "Opened: " << fileOut;
    10. }
    To copy to clipboard, switch view to plain text mode 
    outputs:
    Opened: "C:/Qt/.../some_file_name.dat"
    Windows can handle slash (c:/path/to/file.txt) and backslash (c:\path\to\file.txt) (try it in explorer).

    You can even do something crazy like this:
    Qt Code:
    1. QString fileOut = outputDir+ "\\" +fileName;
    To copy to clipboard, switch view to plain text mode 
    outputs:
    Opened: "C:/Qt/...\some_file_name.dat"

    If you use QtCreator do Build->Clean All / Clean Project (maybe some moc file are interfering), check if file is not used by any other app (if You work with existing files), check Your permission (and app) to the directory that You write file into.
    Last edited by Talei; 8th August 2011 at 20:37.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to set QFile file name with path with windows separators "\"?

    Modern Windows can work with slash separators too so that shouldn't be a problem. If you want to be supercareful then pass QDir::separator() instead of "/".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to set QFile file name with path with windows separators "\"?

    Separators are “treated” automatically by Qt. I’ve changed file name for Windows and now it works.
    Thanks for your help. Solved.

Similar Threads

  1. Absolute Path to users "Program Files" on Windows
    By MisterIKS in forum Qt Programming
    Replies: 6
    Last Post: 5th June 2010, 09:40
  2. Add "separators" in QTreeWidget
    By lipha in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2009, 18:32
  3. Replies: 3
    Last Post: 28th March 2009, 15:37
  4. Replies: 3
    Last Post: 8th July 2008, 19:37
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

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.