Qt對WIN目錄的操作(包括子目錄)
阿新 • • 發佈:2020-08-20
// 測試目錄檔案如下:
// XML
// XML\中朝
// XML\中朝\中朝近
// XML\中朝\中朝近\中朝近.txt
// XML\中朝\檔案3.txt
// XML\中印
// XML\中印\中印內
// XML\中印\中印內\中印內.txt
// XML\中印\檔案2.txt
// XML\檔案1.txt
.pro
1 QT += core gui 2 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 5 CONFIG += c++11 6 7 # The following define makes your compiler emit warnings ifView Codeyou use 8 # any Qt feature that has been marked deprecated (the exact warnings 9 # depend on your compiler). Please consult the documentation of the 10 # deprecated API in order to know how to port your code away from it. 11 DEFINES += QT_DEPRECATED_WARNINGS 12 13 # You can also make your code fail to compile ifit uses deprecated APIs. 14 # In order to do so, uncomment the following line. 15 # You can also select to disable deprecated APIs only up to a certain version of Qt. 16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 18 SOURCES += \ 19 main.cpp \20 mainwindow.cpp 21 22 HEADERS += \ 23 mainwindow.h 24 25 FORMS += \ 26 mainwindow.ui 27 28 # Default rules for deployment. 29 qnx: target.path = /tmp/$${TARGET}/bin 30 else: unix:!android: target.path = /opt/$${TARGET}/bin 31 !isEmpty(target.path): INSTALLS += target
main.cpp
1 #include "mainwindow.h" 2 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }View Code
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 6 #include <QApplication> 7 #include <QDir> 8 #include <QDebug> 9 #include <QStandardItemModel> 10 #include <QStandardItem> 11 12 #include "AllFilesOrPathInDir.h" 13 14 QT_BEGIN_NAMESPACE 15 namespace Ui { class MainWindow; } 16 QT_END_NAMESPACE 17 18 class MainWindow : public QMainWindow 19 { 20 Q_OBJECT 21 22 public: 23 MainWindow(QWidget *parent = nullptr); 24 ~MainWindow(); 25 26 private slots: 27 void on_pushButton_clicked(); 28 29 void on_pushButton_2_clicked(); 30 31 void on_pushButton_3_clicked(); 32 33 void on_pushButton_4_clicked(); 34 35 void on_pushButton_5_clicked(); 36 37 void on_pushButton_6_clicked(); 38 39 void on_pushButton_7_clicked(); 40 41 void on_pushButton_8_clicked(); 42 43 void on_pushButton_9_clicked(); 44 45 private: 46 Ui::MainWindow *ui; 47 AllFilesOrPathInDir *m_pAllFileDir; 48 QString m_sPath; 49 QStringList m_oFilePathNames; 50 QStandardItemModel *m_pItemModel; 51 52 }; 53 #endif // MAINWINDOW_HView Code
mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 // 測試目錄檔案如下: 5 // XML 6 // XML\中朝 7 // XML\中朝\中朝近 8 // XML\中朝\中朝近\中朝近.txt 9 // XML\中朝\檔案3.txt 10 // XML\中印 11 // XML\中印\中印內 12 // XML\中印\中印內\中印內.txt 13 // XML\中印\檔案2.txt 14 // XML\檔案1.txt 15 16 MainWindow::MainWindow(QWidget *parent) 17 : QMainWindow(parent) 18 , ui(new Ui::MainWindow) 19 { 20 ui->setupUi(this); 21 22 m_sPath = QCoreApplication::applicationDirPath() + "/XML"; 23 m_pAllFileDir = new AllFilesOrPathInDir(m_sPath); 24 m_pItemModel = new QStandardItemModel(this); 25 ui->listView->setModel(m_pItemModel); 26 } 27 28 MainWindow::~MainWindow() 29 { 30 delete m_pItemModel; 31 delete ui; 32 } 33 34 void MainWindow::on_pushButton_clicked() 35 { 36 QString dirpath = QCoreApplication::applicationDirPath() + "/XML"; 37 //設定要遍歷的目錄 38 QDir dir(dirpath); 39 //設定檔案過濾器 40 QStringList nameFilters; 41 //設定檔案過濾格式 42 nameFilters << "*.txt"; 43 //將過濾後的檔名稱存入到files列表中 44 QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name); 45 46 // 47 QStandardItemModel *oItemModel = new QStandardItemModel(this); 48 for(int i = 0; i < files.count(); i++) 49 { 50 QStandardItem *oItem = new QStandardItem(files[i]); 51 oItemModel->appendRow(oItem); 52 } 53 ui->listView->setModel(oItemModel); 54 // 結果 55 // 檔案1.txt 56 } 57 58 void MainWindow::on_pushButton_2_clicked() 59 { 60 // 輸出結果 61 m_pItemModel->clear(); 62 // 擷取到的是資料夾的路徑 63 m_oFilePathNames = m_pAllFileDir->getFilePathNameOfSplDir(m_sPath, true); 64 for(int i = 0; i < m_oFilePathNames.count(); i++) 65 { 66 QStandardItem *oItem = new QStandardItem(m_oFilePathNames[i]); 67 m_pItemModel->appendRow(oItem); 68 } 69 // 擷取的是資料夾所在路徑 70 m_oFilePathNames = m_pAllFileDir->getFilePathNameOfSplDir(m_sPath, false); 71 for(int i = 0; i < m_oFilePathNames.count(); i++) 72 { 73 QStandardItem *oItem = new QStandardItem(m_oFilePathNames[i]); 74 m_pItemModel->appendRow(oItem); 75 } 76 // 結果 77 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/檔案1.txt 78 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML 79 } 80 81 void MainWindow::on_pushButton_3_clicked() 82 { 83 // 輸出結果 84 m_pItemModel->clear(); 85 // 輸出結果 86 m_oFilePathNames = m_pAllFileDir->getDirPathOfSplDir(m_sPath); 87 for(int i = 0; i < m_oFilePathNames.count(); i++) 88 { 89 QStandardItem *oItem = new QStandardItem(m_oFilePathNames[i]); 90 m_pItemModel->appendRow(oItem); 91 } 92 // 結果 93 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中印 94 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中朝 95 } 96 97 void MainWindow::on_pushButton_4_clicked() 98 { 99 // 輸出結果 100 m_pItemModel->clear(); 101 // 輸出結果 102 m_oFilePathNames = m_pAllFileDir->getFileAndDirPathNameOfSplDir(m_sPath); 103 for(int i = 0; i < m_oFilePathNames.count(); i++) 104 { 105 QStandardItem *oItem = new QStandardItem(m_oFilePathNames[i]); 106 m_pItemModel->appendRow(oItem); 107 } 108 // 結果 109 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/檔案1.txt 110 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中印 111 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中朝 112 } 113 114 void MainWindow::on_pushButton_5_clicked() 115 { 116 QString sCount = QStringLiteral("檔案數量為:%1 "); 117 ui->label->setText(sCount.arg(m_pAllFileDir->fileConutInSplDir(m_sPath))); 118 } 119 120 void MainWindow::on_pushButton_6_clicked() 121 { 122 QString sCount = QStringLiteral("目錄數量為:%1 "); 123 ui->label_2->setText(sCount.arg(m_pAllFileDir->dirCountInSplDir(m_sPath))); 124 } 125 126 void MainWindow::on_pushButton_7_clicked() 127 { 128 // 輸出結果 129 m_pItemModel->clear(); 130 // 輸出結果 131 m_oFilePathNames = m_pAllFileDir->getFilePathNameOfSplAndChildDir(m_sPath); 132 for(int i = 0; i < m_oFilePathNames.count(); i++) 133 { 134 QStandardItem *oItem = new QStandardItem(m_oFilePathNames[i]); 135 m_pItemModel->appendRow(oItem); 136 } 137 // 結果 138 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/檔案1.txt 139 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中印/檔案2.txt 140 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中朝/檔案3.txt 141 } 142 143 void MainWindow::on_pushButton_8_clicked() 144 { 145 // 輸出結果 146 m_pItemModel->clear(); 147 // 輸出結果 148 m_oFilePathNames = m_pAllFileDir->getDirPathOfSplAndChildDir(m_sPath); 149 for(int i = 0; i < m_oFilePathNames.count(); i++) 150 { 151 QStandardItem *oItem = new QStandardItem(m_oFilePathNames[i]); 152 m_pItemModel->appendRow(oItem); 153 } 154 // 結果 155 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中印 156 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中朝 157 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中印/中印內 158 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中朝/中朝近 159 } 160 161 void MainWindow::on_pushButton_9_clicked() 162 { 163 // 輸出結果 164 m_pItemModel->clear(); 165 // 輸出結果 166 m_oFilePathNames = m_pAllFileDir->getFileAndDirPathNameOfSplAndChildDir(m_sPath); 167 for(int i = 0; i < m_oFilePathNames.count(); i++) 168 { 169 QStandardItem *oItem = new QStandardItem(m_oFilePathNames[i]); 170 m_pItemModel->appendRow(oItem); 171 } 172 // 結果 173 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/檔案1.txt 174 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中印 175 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中印/檔案2.txt 176 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中印/中印內 177 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中印/中印內/中印內.txt 178 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中朝 179 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中朝/檔案3.txt 180 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中朝/中朝近 181 // E:/QtDemoRelease/QtGetAllFileAndDir/QtGetAllFileAndDir/bin/XML/中朝/中朝近/中朝近.txt 182 }View Code
AllFilesOrPathInDir.h
1 #ifndef ALLFILESORPATHINDIR_H 2 #define ALLFILESORPATHINDIR_H 3 4 #include <QDir> 5 #include <QString> 6 #include <QStringList> 7 8 class AllFilesOrPathInDir 9 { 10 public: 11 AllFilesOrPathInDir(); 12 /**建構函式 目錄的路徑 */ 13 AllFilesOrPathInDir(QString dirPath); 14 /** 設定目錄 目標資料夾的目錄 */ 15 void setDirPath(QString dirPath); 16 /** 獲取指定目錄下的檔案路徑+名稱 目標資料夾的目錄 */ 17 QStringList getFilePathNameOfSplDir(QString dirPath, bool isShowFile); 18 /** 獲取在指定目錄下的目錄的路徑 */ 19 QStringList getDirPathOfSplDir(QString dirPath); 20 /** 獲取指定目錄下的檔案和目錄的全路徑 */ 21 QStringList getFileAndDirPathNameOfSplDir(QString dirPath); 22 /** 在指定資料夾下的檔案數 */ 23 int fileConutInSplDir(QString dirPath); 24 /** 在指定資料夾下的目錄數 */ 25 int dirCountInSplDir(QString dirPath); 26 /** 獲取指定目錄及其子目錄下檔案的全路徑 */ 27 QStringList getFilePathNameOfSplAndChildDir(QString dirPath); 28 /** 獲取指定目錄及其子目錄下的資料夾的全路徑 */ 29 QStringList getDirPathOfSplAndChildDir(QString dirPath); 30 /** 獲取指定目錄及其子目錄下的檔案與目錄的全路徑 */ 31 QStringList getFileAndDirPathNameOfSplAndChildDir(QString dirPath); 32 private: 33 QString dirPath; 34 }; 35 36 #endif // ALLFILESORPATHINDIR_HView Code
AllFilesOrPathInDir.cpp
1 #include "AllFilesOrPathInDir.h" 2 3 AllFilesOrPathInDir::AllFilesOrPathInDir() 4 { 5 6 } 7 8 AllFilesOrPathInDir::AllFilesOrPathInDir(QString dirPath) 9 { 10 this->dirPath = dirPath; 11 } 12 13 void AllFilesOrPathInDir::setDirPath(QString dirPath) 14 { 15 this->dirPath = dirPath; 16 } 17 18 QStringList AllFilesOrPathInDir::getFilePathNameOfSplDir(QString dirPath, bool isShowFile) 19 { 20 QStringList filePathNames; 21 QDir splDir(dirPath); 22 QFileInfoList fileInfoListInSplDir = splDir.entryInfoList(QDir::Files); 23 QFileInfo tempFileInfo; 24 foreach (tempFileInfo, fileInfoListInSplDir) { 25 // absoluteFilePath() -- 擷取到的是資料夾的路徑 26 // absolutePath() -- 擷取的是資料夾所在路徑 27 if (isShowFile) 28 { 29 filePathNames << tempFileInfo.absoluteFilePath(); 30 } 31 else 32 { 33 filePathNames << tempFileInfo.absolutePath(); 34 } 35 } 36 return filePathNames; 37 } 38 39 QStringList AllFilesOrPathInDir::getDirPathOfSplDir(QString dirPath) 40 { 41 QStringList dirPaths; 42 QDir splDir(dirPath); 43 QFileInfoList fileInfoListInSplDir = splDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); 44 QFileInfo tempFileInfo; 45 foreach (tempFileInfo, fileInfoListInSplDir) { 46 dirPaths << tempFileInfo.absoluteFilePath(); 47 } 48 return dirPaths; 49 } 50 51 QStringList AllFilesOrPathInDir::getFileAndDirPathNameOfSplDir(QString dirPath) 52 { 53 QStringList fileAndDirPathNames; 54 fileAndDirPathNames << getFilePathNameOfSplDir(dirPath, true); 55 fileAndDirPathNames << getDirPathOfSplDir(dirPath); 56 return fileAndDirPathNames; 57 } 58 59 int AllFilesOrPathInDir::fileConutInSplDir(QString dirPath) 60 { 61 return QDir(dirPath).entryList(QDir::Files).count(); 62 } 63 64 int AllFilesOrPathInDir::dirCountInSplDir(QString dirPath) 65 { 66 return QDir(dirPath).entryList(QDir::Dirs | QDir::NoDotAndDotDot).count(); 67 } 68 69 QStringList AllFilesOrPathInDir::getFilePathNameOfSplAndChildDir(QString dirPath) 70 { 71 QStringList filePathNames; 72 // 首先,得到這個目錄下面的檔案全部 73 filePathNames << getFilePathNameOfSplDir(dirPath, true); 74 75 QStringList childDirs; 76 childDirs << getDirPathOfSplDir(dirPath); 77 78 QString tempChildDir; 79 foreach (tempChildDir, childDirs) { 80 // 取其子資料夾內容 81 filePathNames << getFilePathNameOfSplAndChildDir(tempChildDir); 82 } 83 return filePathNames; 84 } 85 86 QStringList AllFilesOrPathInDir::getDirPathOfSplAndChildDir(QString dirPath) 87 { 88 QStringList dirPathNames; 89 dirPathNames << getDirPathOfSplDir(dirPath); 90 91 QString tempChildDir; 92 foreach (tempChildDir, dirPathNames) { 93 dirPathNames << getDirPathOfSplAndChildDir(tempChildDir); 94 } 95 return dirPathNames; 96 } 97 98 QStringList AllFilesOrPathInDir::getFileAndDirPathNameOfSplAndChildDir(QString dirPath) 99 { 100 QStringList fileAndDirPathNames; 101 fileAndDirPathNames << getFilePathNameOfSplDir(dirPath, true); 102 103 QStringList childDirs; 104 childDirs << getDirPathOfSplDir(dirPath); 105 106 QString tempChildDir; 107 foreach (tempChildDir, childDirs) { 108 fileAndDirPathNames << tempChildDir; 109 // 取其子資料夾內容 110 fileAndDirPathNames << getFileAndDirPathNameOfSplAndChildDir(tempChildDir); 111 } 112 return fileAndDirPathNames; 113 }View Code