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. newbie question: why QT specific file IO classes instead of generic c++ methods?
Forum Updated to NodeBB v4.3 + New Features

newbie question: why QT specific file IO classes instead of generic c++ methods?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 188 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.
  • B Offline
    B Offline
    Bubu 0
    wrote on last edited by VRonin
    #1

    Hi
    I just started with Qt and the first tutorial: Text Finder. Why is QFile used instead some generic c++ classes for file I/O?

    void TextFinder::loadTextFile()
    {
        QFile inputFile(":/input.txt");
        inputFile.open(QIODevice::ReadOnly);
    
        QTextStream in(&inputFile);
        QString line = in.readAll();
        inputFile.close();
    
        ui->textEdit->setPlainText(line);
        QTextCursor cursor = ui->textEdit->textCursor();
        cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
    }
    
    JonBJ W 2 Replies Last reply
    0
    • B Bubu 0

      Hi
      I just started with Qt and the first tutorial: Text Finder. Why is QFile used instead some generic c++ classes for file I/O?

      void TextFinder::loadTextFile()
      {
          QFile inputFile(":/input.txt");
          inputFile.open(QIODevice::ReadOnly);
      
          QTextStream in(&inputFile);
          QString line = in.readAll();
          inputFile.close();
      
          ui->textEdit->setPlainText(line);
          QTextCursor cursor = ui->textEdit->textCursor();
          cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Bubu-0
      Because Qt is a framework, with lots & lots of classes/functions of its own. Offering different functionality compared to "generic c++ classes for file I/O".

      There are lots of classes like this you come across in Qt, e.g. QString instead of std::string etc.

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        QFile internally uses those native methods (see https://code.woboq.org/qt5/qtbase/src/corelib/io/qfsfileengine_unix.cpp.html#214 usinf fread) but takes away a lot of the overhead like managing file flags on unix and handling errors

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • B Bubu 0

          Hi
          I just started with Qt and the first tutorial: Text Finder. Why is QFile used instead some generic c++ classes for file I/O?

          void TextFinder::loadTextFile()
          {
              QFile inputFile(":/input.txt");
              inputFile.open(QIODevice::ReadOnly);
          
              QTextStream in(&inputFile);
              QString line = in.readAll();
              inputFile.close();
          
              ui->textEdit->setPlainText(line);
              QTextCursor cursor = ui->textEdit->textCursor();
              cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
          }
          
          W Offline
          W Offline
          wrosecrans
          wrote on last edited by
          #4

          @Bubu-0 said in newbie question: why QT specific file IO classes instead of generic c++ methods?:

          QFile inputFile(":/input.txt");

          If you use Qt's resource system (file path starts with ":" ) to access files that are baked into the executable, Qt's file API's are the only thing that will do it.

          If you just want to access a file on the disk, you can use whatever you want. But obviously the Qt tutorials are going to be tutorials in the Qt API's, not generic C++ lessons.

          1 Reply Last reply
          1

          • Login

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