Results 1 to 3 of 3

Thread: QTemporaryFile and Qfile::copy()

  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Question QTemporaryFile and Qfile::copy()

    Hi Guys,

    I have a need to copy an existing file, or resource, into a temporary file. I can use the QTemporaryFile to create an empty temporary file in a safe fashion. However, to use QFile::copy() to move my data I need the target file to not exist. If I grab the fileName() and remove the temp file then I have put myself back into the race condition that tmpnam (3) warns against and that QTemporaryFile is designed to avoid. There does not seem to be a file copy that uses open QFile (QIODevice) as source and target.

    Is there an elegant QT approach to this problem?

    Cheers,
    Chris

    Edit: I can use QTemporaryFile::createLocalFile() on the resource to get the copy on to disc, but this will not copy an already local file.
    Last edited by ChrisW67; 22nd April 2009 at 02:11.

  2. #2
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTemporaryFile and Qfile::copy()

    Apparently the only way to do it is to manually read the data from the source file and write it to the temporary file using QIODevice's read/write methods.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTemporaryFile and Qfile::copy()

    Thanks. That's the conclusion I had reached also.

Tags for this Thread

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.