qt获取当前文件夹下的所有文件
时间: 2023-08-08 21:10:12 浏览: 698
可以使用QDir类来获取当前文件夹下的所有文件。具体实现可以参考以下代码:
```
QDir dir(".");
QStringList files = dir.entryList(QDir::Files);
foreach(QString file, files) {
qDebug() << file;
}
```
上述代码中,`QDir`的构造函数参数为`.`,表示当前文件夹。`entryList()`函数可以获取当前文件夹下的所有文件,其中`QDir::Files`表示只获取文件类型的条目。最后使用`foreach`遍历`files`列表并打印出文件名。
相关问题
Qt遍历当前文件夹所有文件
以下是一个示例代码,使用Qt遍历当前文件夹所有文件:
```
#include <QDir>
#include <QDebug>
void traverseFiles(const QDir& dir)
{
QStringList filters;
filters << "*.txt"; //可以根据需要修改过滤条件,这里只列出txt文件
QFileInfoList list = dir.entryInfoList(filters, QDir::Files);
foreach(QFileInfo fileInfo, list)
{
qDebug() << fileInfo.filePath();
}
QStringList subDirList = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
foreach(QString subDir, subDirList)
{
traverseFiles(QDir(dir.filePath(subDir)));
}
}
int main(int argc, char *argv[])
{
QDir dir(".");
traverseFiles(dir);
return 0;
}
```
上述代码中,`traverseFiles`函数用于遍历当前文件夹中的所有文件和子文件夹。首先,使用`entryInfoList`函数获取当前文件夹中所有符合过滤条件(这里是txt文件)的文件,然后遍历输出每个文件的路径。接着,使用`entryList`函数获取当前文件夹中所有子文件夹的名称,然后递归调用`traverseFiles`函数遍历每个子文件夹。
Qt搜索当前文件夹下所有不为空的文件夹
可以使用Qt的QDir类和QFileInfo类来实现搜索当前文件夹下所有不为空的文件夹。
具体步骤如下:
1. 创建一个QDir对象,指定要搜索的文件夹路径。
```
QDir dir("/path/to/folder");
```
2. 使用QDir的entryInfoList()函数获取文件夹中所有文件和子文件夹的信息,存储到QFileInfoList对象中。
```
QFileInfoList fileList = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
```
3. 遍历QFileInfoList对象,筛选出不为空的文件夹。
```
foreach (QFileInfo fileInfo, fileList) {
if (fileInfo.isDir()) { // 判断是否为文件夹
QDir subDir(fileInfo.absoluteFilePath()); // 创建子文件夹的QDir对象
if (subDir.count() > 2) { // 判断子文件夹是否为空
qDebug() << "Folder" << fileInfo.fileName() << "is not empty.";
}
}
}
```
完整代码如下:
```
QDir dir("/path/to/folder");
QFileInfoList fileList = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
foreach (QFileInfo fileInfo, fileList) {
if (fileInfo.isDir()) {
QDir subDir(fileInfo.absoluteFilePath());
if (subDir.count() > 2) {
qDebug() << "Folder" << fileInfo.fileName() << "is not empty.";
}
}
}
```
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)