QDir的常用操作
QDir 是 Qt 中用于目录操作的类,提供了创建、删除、遍历目录、获取目录信息等功能。以下是 QDir 的常用操作及其示例代码。
1. 创建和删除目录
创建目录
QDir dir("/path/to/directory");
if (dir.mkdir("newdir")) {
qDebug() << "Directory created successfully.";
} else {
qDebug() << "Failed to create directory:" << dir.errorString();
}
递归创建目录
QDir dir("/path/to/directory");
if (dir.mkpath("newdir/subdir")) {
qDebug() << "Directory created successfully.";
} else {
qDebug() << "Failed to create directory:" << dir.errorString();
}
删除目录
QDir dir("/path/to/directory");
if (dir.rmdir("newdir")) {
qDebug() << "Directory removed successfully.";
} else {
qDebug() << "Failed to remove directory:" << dir.errorString();
}
递归删除目录
QDir dir("/path/to/directory");
if (dir.removeRecursively()) {
qDebug() << "Directory removed successfully.";
} else {
qDebug() << "Failed to remove directory:" << dir.errorString();
}
2. 遍历目录
获取目录下的所有文件和子目录
QDir dir("/path/to/directory");
QStringList entries = dir.entryList();
for (const QString& entry : entries) {
qDebug() << entry;
}
过滤文件和子目录
QDir dir("/path/to/directory");
QStringList filters;
filters << "*.txt" << "*.cpp"; // 过滤扩展名为 .txt 和 .cpp 的文件
QStringList files = dir.entryList(filters, QDir::Files);
for (const QString& file : files) {
qDebug() << file;
}
遍历子目录
QDir dir("/path/to/directory");
QStringList dirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for (const QString& subdir : dirs) {
qDebug() << subdir;
}
3. 获取目录信息
获取当前工作目录
QString currentPath = QDir::currentPath();
qDebug() << "Current directory:" << currentPath;
设置当前工作目录
if (QDir::setCurrent("/path/to/directory")) {
qDebug() << "Current directory changed successfully.";
} else {
qDebug() << "Failed to change directory.";
}
获取目录的绝对路径
QDir dir("relative/path");
QString absolutePath = dir.absolutePath();
qDebug() << "Absolute path:" << absolutePath;
获取目录的规范路径(去除冗余部分)
QDir dir("/path/to/../directory");
QString canonicalPath = dir.canonicalPath();
qDebug() << "Canonical path:" << canonicalPath;
4. 检查目录状态
检查目录是否存在
QDir dir("/path/to/directory");
if (dir.exists()) {
qDebug() << "Directory exists.";
} else {
qDebug() << "Directory does not exist.";
}
检查是否为根目录
QDir dir("/");
if (dir.isRoot()) {
qDebug() << "This is the root directory.";
} else {
qDebug() << "This is not the root directory.";
}
5. 目录路径操作
获取目录名
QDir dir("/path/to/directory");
QString dirName = dir.dirName();
qDebug() << "Directory name:" << dirName;
获取父目录
QDir dir("/path/to/directory");
QDir parentDir = dir;
if (parentDir.cdUp()) {
qDebug() << "Parent directory:" << parentDir.absolutePath();
} else {
qDebug() << "Failed to get parent directory.";
}
拼接路径
QDir dir("/path/to");
QString newPath = dir.filePath("directory/file.txt");
qDebug() << "New path:" << newPath;
6. 文件系统操作
获取磁盘空间信息
QDir dir("/");
qint64 totalSpace = dir.root().rootPath().toLongLong();
qint64 freeSpace = dir.root().rootPath().toLongLong();
qDebug() << "Total space:" << totalSpace << "bytes";
qDebug() << "Free space:" << freeSpace << "bytes";
7. 目录权限设置
设置目录权限
QDir dir("/path/to/directory");
if (dir.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner)) {
qDebug() << "Directory permissions set successfully.";
} else {
qDebug() << "Failed to set directory permissions:" << dir.errorString();
}
8. 目录重命名
重命名目录
QDir dir("/path/to/directory");
if (dir.rename("olddir", "newdir")) {
qDebug() << "Directory renamed successfully.";
} else {
qDebug() << "Failed to rename directory:" << dir.errorString();
}
9. 目录复制
复制目录
QDir srcDir("/path/to/srcdir");
QDir destDir("/path/to/destdir");
if (srcDir.copy("srcdir", destDir.filePath("destdir"))) {
qDebug() << "Directory copied successfully.";
} else {
qDebug() << "Failed to copy directory:" << srcDir.errorString();
}
10. 目录错误处理
获取错误信息
QDir dir("/path/to/nonexistent");
if (!dir.exists()) {
qDebug() << "Error:" << dir.errorString();
}
总结
QDir 提供了丰富的功能来操作目录,包括创建、删除、遍历、获取信息、路径操作、权限设置、重命名、复制和错误处理等。掌握这些操作可以轻松处理文件系统任务。
浙公网安备 33010602011771号