Results 1 to 3 of 3

Thread: QFile or QDir rename()

  1. #1
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default QFile or QDir rename()

    When I use on of the rename functions and a file is open by an outside program, a new file is created with the new name instead of the rename function returning false as i had hoped. Is there a way around this? A way to check if the file is open elsewhere? I tried using QFile:: open() but that still opened and then corrupted my file.

    Qt 4.4.3 on Windows 2003.

    mAx
    Last edited by maxpower; 23rd January 2009 at 15:54.

  2. #2
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFile or QDir rename()

    Qt tries to be smart and emulate unix-like rename across directories on windows, e.g. when operating system rename fails it tries a copy/delete, but does not check completely for success on deleting the old file.

    As a workaround I suggest to make your own function. Something along this should work (did not test myself though):

    Qt Code:
    1. #include "qt_windows.h"
    2.  
    3. bool winRenameFile(const QString& aOldName, const QString& aNewName)
    4. {
    5. return MoveFile((LPWSTR)aOldName.utf16(), (LPWSTR)aNewName.utf16());
    6. } // winRenameFile
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to seneca for this useful post:

    maxpower (23rd January 2009)

  4. #3
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QFile or QDir rename()

    Thanks, that worked as I expected.

    mAx

Similar Threads

  1. QDir and QFile
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 27th September 2007, 13:43
  2. How to explicitely delete a QDir?
    By alan in forum Newbie
    Replies: 2
    Last Post: 13th February 2006, 17:48

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.