環境
VS2015 qt5.9.7 64位
包含的頭文件
#include <QDir> #include <QDebug> #include <QMessageBox>
代碼
//--1 判斷文件夾是否存在 QString folderPath = "H:\\Pro"; QDir dir(folderPath); if(!dir.exists()) { QMessageBox::critical(this,tr("錯誤"),tr("文件夾找不到")); return; } //--2 獲取當前路徑下所有的文件夾名字 // -- 注:QDir::Dirs 為獲取所有文件夾名稱,獲取文件名稱需要修改 QStringList names = dir.entryList(QDir::Dirs); //--3 刪除當前文件夾和上級文件夾(溫馨提示:隱藏的文件夾獲取不了) names.removeOne("."); names.removeOne(".."); //--4 打印出獲取的文件名 qDebug() << "names: " << names;