newbie question: why QT specific file IO classes instead of generic c++ methods?
-
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); }
-
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); }
@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 ofstd::string
etc. -
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 -
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); }
@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.