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?
Forum Updated to NodeBB v4.3 + New Features

how to use AutoCloseHandle in QFile base operation?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 337 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 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;
                }
    
    JonBJ 1 Reply Last reply
    0
    • Q Qt embedded developer

      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;
                  }
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on 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
      2
      • JonBJ JonB

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

          • Login

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