Turn QTemporaryFile into permanent
-
Hi,
I have an app that creates a recording session using a temp file. One the user clicks on "save as", I'd like to turn that temporary file into a permanent one, using the path provided by the user.Is there a way or a trick to turn QTemporaryFile into a permanent QFile ? I'd like to avoid copying data to new location, closing the temp file and opening the new file: this will trigger "open" app logic which clears the current data to display the new ones.
Thanks
-
Hi,
I have an app that creates a recording session using a temp file. One the user clicks on "save as", I'd like to turn that temporary file into a permanent one, using the path provided by the user.Is there a way or a trick to turn QTemporaryFile into a permanent QFile ? I'd like to avoid copying data to new location, closing the temp file and opening the new file: this will trigger "open" app logic which clears the current data to display the new ones.
Thanks
@dextermagnific said in Turn QTemporaryFile into permanent:
I'd like to avoid copying data to new location
Then move it - as long as it is on same partition it is cheap. See https://doc.qt.io/qt-6/qtemporaryfile.html#rename
If user selects a location on a different volume/partition/drive then you have to copy. -
Hi,
I have an app that creates a recording session using a temp file. One the user clicks on "save as", I'd like to turn that temporary file into a permanent one, using the path provided by the user.Is there a way or a trick to turn QTemporaryFile into a permanent QFile ? I'd like to avoid copying data to new location, closing the temp file and opening the new file: this will trigger "open" app logic which clears the current data to display the new ones.
Thanks
@dextermagnific
You would have to either copy or rename it before theQTemporaryFilevariable goes out of scope. Whether that will work or cause a problem I don't know (certainly you should try closing it before copy/rename). See https://stackoverflow.com/questions/25015392/qt-copy-to-qtemporaryfile for possible caveats, though that is copying to the file rather than from it. The final post there, suggesting to use your own UUID-unique-generated filename instead, might be relevant to you. -
@dextermagnific said in Turn QTemporaryFile into permanent:
I'd like to avoid copying data to new location
Then move it - as long as it is on same partition it is cheap. See https://doc.qt.io/qt-6/qtemporaryfile.html#rename
If user selects a location on a different volume/partition/drive then you have to copy.@jsulm said in Turn QTemporaryFile into permanent:
Then move it - as long as it is on same partition it is cheap. See https://doc.qt.io/qt-6/qtemporaryfile.html#rename
Attention: according to the docs this will fail explicitly for QTemporaryFile when it's on another partition.
-
If you create a file, even when it is a temporary file, it will be a file located on the disk. There is only two things you can do: either move/rename the file or copy the file. There is nothing Qt can do to circumvent that.
This leaves the options already suggested before. I like the idea of QTemporaryFile::rename. You can call that function first and check its return value. If it didn't succeed you can still copy the file instead in the regular way (using QFile::copy).
-
If you create a file, even when it is a temporary file, it will be a file located on the disk. There is only two things you can do: either move/rename the file or copy the file. There is nothing Qt can do to circumvent that.
This leaves the options already suggested before. I like the idea of QTemporaryFile::rename. You can call that function first and check its return value. If it didn't succeed you can still copy the file instead in the regular way (using QFile::copy).
@SimonSchroeder
QTemporaryFile::rename() documentation is quite silent on whether the renamed file will or will not be deleted when theQTemporaryFileinstance is destroyed, as is the whole point certainly if it is not renamed. I don't know whether it notes the rename and deletes it under its new name, do you? -
Thank you for all the replies. I'll go with the rename+copy fallback. In doubt I'll disable auto delete.