Qt5 Tutorial QDir - 2020
In this tutorial, we will learn QDir.
The QDir class provides access to directory structures and their contents.
A QDir is used to manipulate path names, access information regarding paths and files, and manipulate the underlying file system. It can also be used to access Qt's resource system.
Qt uses "/" as a universal directory separator in the same way that "/" is used as a path separator in URLs. If you always use "/" as a directory separator, Qt will translate your paths to conform to the underlying operating system.
The following code checks if the two directories exist. In this example, the first one does, but the second one does not:
#include <QCoreApplication> #include <QDir> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // forward slash for directory separator QDir dir1("C:/Qt"); QDir dir2("C:/Qt/test"); // output: true false qDebug() << dir1.exists() << dir2.exists(); return a.exec(); }
QFileInfoList QDir::drives() returns a list of the root directories on this system.
On Windows this returns a list of QFileInfo objects containing "C:/", "D:/", etc. On other operating systems, it returns a list containing just one root directory (i.e. "/").
#include <QCoreApplication> #include <QDir> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // forward slash for directory separator QDir dir1("C:/Qt"); QDir dir2("C:/Qt/test"); qDebug() << dir1.exists() << dir2.exists(); QDir dir3; foreach(QFileInfo item, dir3.drives() ) { qDebug() << item.absoluteFilePath(); } return a.exec(); }
Output:
true false "C:/" "D:/" "E:/"
bool QDir::mkpath(const QString & dirPath) const creates the directory path dirPath.
The function will create all parent directories necessary to create the directory.
mkpath() returns true if successful; otherwise returns false.
If the path already exists when this function is called, it will return true.
The code below checks if a directory exists. In this example, a new directory will be created because it does not exist. If we run it again, the code will give a message telling the directory already exists.
#include <QCoreApplication> #include <QDir> #include <QDebug> #include <QString> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString path = "C:/Qt/test"; QDir dir(path); if(!dir.exists()) { qDebug() << "Creating " << path << "directory"; dir.mkpath(path); } else { qDebug() << path << " already exists"; } return a.exec(); }
If we run the code, we get the output:
Creating "C:/Qt/test" directory
If we run it again, we get the following message:
"C:/Qt/test" already exists
QFileInfoList QDir::entryInfoList(const QStringList & nameFilters, Filters filters = NoFilter, SortFlags sort = NoSort) const returns a list of QFileInfo objects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting().
The name filter, file attribute filter, and sorting specification can be overridden using the nameFilters, filters, and sort arguments.
entryInfoList() returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification.
Here is the code using QDir::entryInfoList():
#include <QCoreApplication> #include <QDir> #include <QDebug> #include <QString> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString path = "C:/Qt"; QDir dir(path); foreach(QFileInfo item, dir.entryInfoList() ) { if(item.isDir()) qDebug() << "Dir: " << item.absoluteFilePath(); if(item.isFile()) qDebug() << "File: " << item.absoluteFilePath(); } return a.exec(); }
Output:
Dir: "C:/Qt" Dir: "C:/" ... Dir: "C:/Qt/build-QDir-Desktop_Qt_5_1_0_MSVC2012_OpenGL_64bit-Debug" ... File: "C:/Qt/FileA.txt" File: "C:/Qt/FileB.txt" ,,, Dir: "C:/Qt/test"
Qt 5 Tutorial
- Hello World
- Signals and Slots
- Q_OBJECT Macro
- MainWindow and Action
- MainWindow and ImageViewer using Designer A
- MainWindow and ImageViewer using Designer B
- Layouts
- Layouts without Designer
- Grid Layouts
- Splitter
- QDir
- QFile (Basic)
- Resource Files (.qrc)
- QComboBox
- QListWidget
- QTreeWidget
- QAction and Icon Resources
- QStatusBar
- QMessageBox
- QTimer
- QList
- QListIterator
- QMutableListIterator
- QLinkedList
- QMap
- QHash
- QStringList
- QTextStream
- QMimeType and QMimeDatabase
- QFile (Serialization I)
- QFile (Serialization II - Class)
- Tool Tips in HTML Style and with Resource Images
- QPainter
- QBrush and QRect
- QPainterPath and QPolygon
- QPen and Cap Style
- QBrush and QGradient
- QPainter and Transformations
- QGraphicsView and QGraphicsScene
- Customizing Items by inheriting QGraphicsItem
- QGraphicsView Animation
- FFmpeg Converter using QProcess
- QProgress Dialog - Modal and Modeless
- QVariant and QMetaType
- QtXML - Writing to a file
- QtXML - QtXML DOM Reading
- QThreads - Introduction
- QThreads - Creating Threads
- Creating QThreads using QtConcurrent
- QThreads - Priority
- QThreads - QMutex
- QThreads - GuiThread
- QtConcurrent QProgressDialog with QFutureWatcher
- QSemaphores - Producer and Consumer
- QThreads - wait()
- MVC - ModelView with QListView and QStringListModel
- MVC - ModelView with QTreeView and QDirModel
- MVC - ModelView with QTreeView and QFileSystemModel
- MVC - ModelView with QTableView and QItemDelegate
- QHttp - Downloading Files
- QNetworkAccessManager and QNetworkRequest - Downloading Files
- Qt's Network Download Example - Reconstructed
- QNetworkAccessManager - Downloading Files with UI and QProgressDialog
- QUdpSocket
- QTcpSocket
- QTcpSocket with Signals and Slots
- QTcpServer - Client and Server
- QTcpServer - Loopback Dialog
- QTcpServer - Client and Server using MultiThreading
- QTcpServer - Client and Server using QThreadPool
- Asynchronous QTcpServer - Client and Server using QThreadPool
- Qt Quick2 QML Animation - A
- Qt Quick2 QML Animation - B
- Short note on Ubuntu Install
- OpenGL with QT5
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part A
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part B
- Video Player with HTML5 QWebView and FFmpeg Converter
- Qt5 Add-in and Visual Studio 2012
- Qt5.3 Installation on Ubuntu 14.04
- Qt5.5 Installation on Ubuntu 14.04
- Short note on deploying to Windows
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization