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. QTemporaryFile: close method
Forum Update on Monday, May 27th 2025

QTemporaryFile: close method

Scheduled Pinned Locked Moved Solved General and Desktop
qtemporaryfilefileclose
8 Posts 3 Posters 3.8k Views
  • 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.
  • M Offline
    M Offline
    Mark81
    wrote on 2 May 2016, 18:23 last edited by Mark81 5 Feb 2016, 18:23
    #1

    I need to create a temporary file with a unique name. But I don't need to open this file within my Qt app, instead I need to pass the filename as an argument for QProcess.
    To get the filename I need to open the QTemporaryFile. It's not a big deal... I might open, get the filename, close and call QProcess.

    But according to the documentation:

    doc.qt.io/qt-5.6/qtemporaryfile.html

    Reopening a QTemporaryFile after calling close() is safe. For as long as the QTemporaryFile object itself is not destroyed, the unique temporary file will exist and be kept open internally by QTemporaryFile.
    

    I'm not sure about: "will exist and be kept open". Does that mean the file is still open even after calling close() ?
    Is it safe to pass the filename to other applications which will use it to write data?

    K 1 Reply Last reply 3 May 2016, 07:48
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 2 May 2016, 21:21 last edited by
      #2

      Hi
      Do you want to use as a filename generator?
      It seems the file is first removed when deleted
      http://doc.qt.io/qt-5.6/qtemporaryfile.html#autoRemove

      M 1 Reply Last reply 3 May 2016, 06:19
      2
      • M mrjj
        2 May 2016, 21:21

        Hi
        Do you want to use as a filename generator?
        It seems the file is first removed when deleted
        http://doc.qt.io/qt-5.6/qtemporaryfile.html#autoRemove

        M Offline
        M Offline
        Mark81
        wrote on 3 May 2016, 06:19 last edited by
        #3

        @mrjj

        Do you want to use as a filename generator?
        

        Exactly!

        It seems the file is first removed when deleted
        http://doc.qt.io/qt-5.6/qtemporaryfile.html#autoRemove
        
        

        When the object is deleted, not when the file is closed. See also:

        http://doc.qt.io/qt-5.6/qtemporaryfile.html#dtor.QTemporaryFile

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 3 May 2016, 06:25 last edited by
          #4

          So in theory, you should be able to use the filename if you call
          remove. OR if you simply delete it.

          M 1 Reply Last reply 3 May 2016, 06:41
          1
          • M mrjj
            3 May 2016, 06:25

            So in theory, you should be able to use the filename if you call
            remove. OR if you simply delete it.

            M Offline
            M Offline
            Mark81
            wrote on 3 May 2016, 06:41 last edited by
            #5

            @mrjj
            I checked into the source code:

            bool QTemporaryFileEngine::close()
            {
                // Don't close the file, just seek to the front.
                seek(0);
                setError(QFile::UnspecifiedError, QString());
                return true;
            }
            

            Thuse, closing the file does not close it. Great.
            I guess it's quite safe to generate the filename, destroy the object and immediately use that file name.
            Because I don't need a lot of concurrent files I bet I won't receive the same filename in two consequent calls!

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 3 May 2016, 07:03 last edited by
              #6

              but try the
              http://doc.qt.io/qt-5.6/qfile.html#remove
              That is what AutoRemove calls on destruction.
              Doc says.

              1 Reply Last reply
              0
              • M Mark81
                2 May 2016, 18:23

                I need to create a temporary file with a unique name. But I don't need to open this file within my Qt app, instead I need to pass the filename as an argument for QProcess.
                To get the filename I need to open the QTemporaryFile. It's not a big deal... I might open, get the filename, close and call QProcess.

                But according to the documentation:

                doc.qt.io/qt-5.6/qtemporaryfile.html

                Reopening a QTemporaryFile after calling close() is safe. For as long as the QTemporaryFile object itself is not destroyed, the unique temporary file will exist and be kept open internally by QTemporaryFile.
                

                I'm not sure about: "will exist and be kept open". Does that mean the file is still open even after calling close() ?
                Is it safe to pass the filename to other applications which will use it to write data?

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 3 May 2016, 07:48 last edited by kshegunov 5 Mar 2016, 07:49
                #7

                @Mark81
                Perhaps you should consider just using the STL, or rather the standard C library? For example std::tmpnam would be a good candidate, wouldn't it?

                Read and abide by the Qt Code of Conduct

                M 1 Reply Last reply 3 May 2016, 09:46
                1
                • K kshegunov
                  3 May 2016, 07:48

                  @Mark81
                  Perhaps you should consider just using the STL, or rather the standard C library? For example std::tmpnam would be a good candidate, wouldn't it?

                  M Offline
                  M Offline
                  Mark81
                  wrote on 3 May 2016, 09:46 last edited by
                  #8

                  @kshegunov I didn't know that function. Thank you very much, I will try it.

                  1 Reply Last reply
                  0

                  3/8

                  3 May 2016, 06:19

                  topic:navigator.unread, 5
                  • Login

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