• Qt对WIN目录的操作(包括子目录)


     

    // 测试目录文件如下:

    // 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 if you 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 if it 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
    View Code

    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_H
    View 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_H
    View 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
    作者:疯狂Delphi
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

    欢迎关注我,一起进步!扫描下方二维码即可加我

  • 相关阅读:
    关于学习Knockoutjs--入门(一)
    h5移动端前端性能优化
    VS2015常用快捷键总结
    51nod1196 字符串的数量
    51nod1189 阶乘分数
    51nod1161 Partial Sums
    51nod1040 矩阵相乘结果的判断
    51nod 1125 交换机器的最小代价
    51nod 1120 机器人走方格 V3
    51nod 1040 最大公约数之和
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/13535527.html
Copyright © 2020-2023  润新知