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. how to use AutoCloseHandle in QFile base operation?

how to use AutoCloseHandle in QFile base operation?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 309 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on 14 Nov 2022, 12:35 last edited by Qt embedded developer
    #1

    i want to know how to use AutoCloseHandle flag of Qfile in below code ?

    QFile file(QDir::homePath() + "/Runtime/"+ "NewFile.txt");
    
                if (!file.open(QIODevice::ReadWrite))
                {
                qDebug() << "file is not exist";
                qDebug()<< "Cannot open file for writing: "
                             << qPrintable(file.errorString());
                }
                else
                {
                    QTextStream out(&file);
                    out << secondsbetweenDateTime<< endl;
                }
    
    J 1 Reply Last reply 14 Nov 2022, 13:15
    0
    • Q Qt embedded developer
      14 Nov 2022, 12:35

      i want to know how to use AutoCloseHandle flag of Qfile in below code ?

      QFile file(QDir::homePath() + "/Runtime/"+ "NewFile.txt");
      
                  if (!file.open(QIODevice::ReadWrite))
                  {
                  qDebug() << "file is not exist";
                  qDebug()<< "Cannot open file for writing: "
                               << qPrintable(file.errorString());
                  }
                  else
                  {
                      QTextStream out(&file);
                      out << secondsbetweenDateTime<< endl;
                  }
      
      J Offline
      J Offline
      JonB
      wrote on 14 Nov 2022, 13:15 last edited by
      #2

      @Qt-embedded-developer
      Why are you interested at all in QFileDevice::AutoCloseHandle? Just for the sake of it? Since the docs state:

      When opening a file by name, this flag is ignored as Qt always owns the file handle and must close it.

      how is it relevant to your situation?

      if (!file.open(QIODevice::ReadWrite))

      As I posted in your previous thread, ReadWrite is likely to be the wrong choice here. Up to you whether you want to respect this or ignore it.

      Q 1 Reply Last reply 15 Nov 2022, 04:23
      2
      • J JonB
        14 Nov 2022, 13:15

        @Qt-embedded-developer
        Why are you interested at all in QFileDevice::AutoCloseHandle? Just for the sake of it? Since the docs state:

        When opening a file by name, this flag is ignored as Qt always owns the file handle and must close it.

        how is it relevant to your situation?

        if (!file.open(QIODevice::ReadWrite))

        As I posted in your previous thread, ReadWrite is likely to be the wrong choice here. Up to you whether you want to respect this or ignore it.

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on 15 Nov 2022, 04:23 last edited by Qt embedded developer
        #3

        @JonB can you tell me what i have to write in place of below

        if (!file.open(QIODevice::ReadWrite))
        

        Because as i want to create the file when file not exist.

        My file is like simple file on home directory of linux .

        I asked question because i want to explore purpose of QFileDevice::AutoCloseHandle .

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on 15 Nov 2022, 04:50 last edited by Bonnie
          #4

          This is only used when you open a file by an existing file handle or file descriptor.

          bool QFile::open(FILE *fh, QIODevice::OpenMode mode, QFileDevice::FileHandleFlags handleFlags = DontCloseHandle)
          bool QFile::open(int fd, QIODevice::OpenMode mode, QFileDevice::FileHandleFlags handleFlags = DontCloseHandle)

          1 Reply Last reply
          0

          4/4

          15 Nov 2022, 04:50

          • Login

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