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
QtWS25 Last Chance

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 last edited by Mark81
    #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?

    kshegunovK 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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
      2
      • mrjjM mrjj

        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 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
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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
          1
          • mrjjM mrjj

            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 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
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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

                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?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #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
                1
                • kshegunovK kshegunov

                  @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 last edited by
                  #8

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

                  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