Use the QDir class in Qt for directory operations

overview

Qt is a cross-platform C++ application development framework that provides many convenient classes to handle file and directory operations. Among them, the QDir class is one of the core classes used to process directories (folders). In this blog post, we will introduce in detail how to use the QDir class for directory operations, and provide some practical application scenarios.

Basic functions of the QDir class

The QDir class provides a series of functions to manipulate directories, including creating directories, listing directory contents, copying files and directories, deleting files and directories, and so on. First, we need to include the header file:


#include <QDir>

get the current directory

We can use QDir's static member function currentPath() to get the working directory of the current application:

QString currentPath = QDir::currentPath();

Create a directory

To create a new directory, we can use the mkdir() function. If the directory already exists, the function returns false, otherwise it returns true.

QDir directory;
if (directory.mkdir("new_directory")) {
    
    
    // 目录创建成功
} else {
    
    
    // 目录已存在或创建失败
}

list directory contents

We can list all the files and subdirectories in a directory using the entryList() function. This function returns a QStringList containing all file and subdirectory names.


QStringList filesAndDirs = directory.entryList();
foreach (const QString& name, filesAndDirs) {
    
    
    qDebug() << name;
}

Filter directory content

To filter files, you can use the overloaded version of the entryList() function and specify a filter to select files of a specific type. The filter is an enumeration type QDir::Filter, which can select files, directories, hidden files, etc. Here is an example showing how to print only files without subdirectories:

#include <QDir>
#include <QDebug>

int main() {
    
    
    QDir directory("/path/to/your/directory"); // 修改为你想遍历的目录路径

    QStringList files = directory.entryList(QDir::Files);
    foreach (const QString& fileName, files) {
    
    
        qDebug() << "File:" << fileName;
    }

    return 0;
}

In this example, we use the QDir::Files filter to only get a list of files in a directory, not subdirectories. This way, the output will only include the files in the directory, ignoring the contents of subdirectories.

In addition to QDir::Files, there are other filters that can be used:

QDir::Dirs:获取目录下的所有子目录。
QDir::AllEntries:获取目录下的所有文件和子目录(包括隐藏文件和隐藏目录)。
QDir::NoDotAndDotDot:排除特殊目录...
QDir::Hidden:获取隐藏的文件和目录。

Filter for specific named files

For example, to filter out .ini files, you can use the overloaded version of the entryList() function and specify a file filter to select only files containing the .ini extension. Here is an example showing how to print out only the .ini files in a directory:


#include <QDir>
#include <QDebug>

int main() {
    
    
    QDir directory("/path/to/your/directory"); // 修改为你想遍历的目录路径

    QStringList iniFiles = directory.entryList(QStringList() << "*.ini", QDir::Files);
    foreach (const QString& iniFile, iniFiles) {
    
    
        qDebug() << "INI File:" << iniFile;
    }

    return 0;
}

Copy files and directories

To copy files or directories, the QFile::copy() and QDir::rename() functions can be used.


QFile::copy("sourceFile.txt", "destinationFile.txt");

QDir directory;
directory.rename("sourceDirectory", "destinationDirectory");

Delete files and directories

We can delete files and directories using the QFile::remove() function and QDir::rmdir() function.


QFile::remove("fileToRemove.txt");

QDir directory;
directory.rmdir("directoryToRemove");

Application Scenario

The QDir class can play a role in many practical application scenarios, such as:

File and directory management: Create, copy, delete, and rename files and directories in the application to realize data management and storage.

Batch processing: When you need to batch process a group of files in a directory, you can use the QDir class to traverse all the files in the directory, and then perform corresponding operations on each file.

Directory comparison: Compare files and subdirectories in two directories, find differences or perform synchronization operations.

File search: Use the entryList() function of the QDir class with regular expressions to search for files that meet specific rules in the directory.

Log management: When recording logs, you can use the QDir class to create and manage log files.

Summarize

The QDir class is an important tool for directory operations in the Qt framework. Through the simple interface, we can easily create, list, copy, delete and other operations of the directory to meet the needs of various practical application scenarios. It makes file and directory management easier and more efficient, and improves the maintainability and scalability of applications. Whether it is a desktop application, a background service or a command-line tool, the QDir class can provide us with powerful support. I hope this blog post can help you better understand and apply the QDir class, and improve your Qt development skills.

おすすめ

転載: blog.csdn.net/qq_46017342/article/details/132051545