Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Turn QTemporaryFile into permanent
Forum Updated to NodeBB v4.3 + New Features

Turn QTemporaryFile into permanent

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 690 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dextermagnific
    wrote on last edited by
    #1

    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

    jsulmJ JonBJ 2 Replies Last reply
    0
    • D dextermagnific

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Christian EhrlicherC 1 Reply Last reply
      0
      • D dextermagnific

        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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @dextermagnific
        You would have to either copy or rename it before the QTemporaryFile variable 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.

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @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.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #5

            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).

            JonBJ 1 Reply Last reply
            0
            • S SimonSchroeder

              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).

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @SimonSchroeder
              QTemporaryFile::rename() documentation is quite silent on whether the renamed file will or will not be deleted when the QTemporaryFile instance 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?

              1 Reply Last reply
              1
              • D Offline
                D Offline
                dextermagnific
                wrote on last edited by
                #7

                Thank you for all the replies. I'll go with the rename+copy fallback. In doubt I'll disable auto delete.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved