QT输入输出(三)之 目录遍历(QDir)

时间:2020-12-26 14:34:15

QDir类提供了访问目录结构和他们的内容的方法。

QDir类提供了访问目录结构和它们的内容的与平台无关的方式。 
QDir用来操作路径名称、关于路径和文件的访问信息和操作底层文件系统。 
QDir使用相对或绝对文件路径来指向一个文件。绝对路径是从目录分隔符“/”或者带有一个驱动器标识(除了在Unix下)。如果你总是使用“/”作为目录分隔符,Qt将会把你的路径转化为符合底层的操作系统的。相对文件名是由一个目录名称或者文件名开始并且指定一个相对于当前路径的路径。 
例如绝对路径:
     QDir("/home/administrator/soft");
     QDir("D:/software");
     我们可以使用isRelative()或isAbsolute()函数确认QDir是用的相对路径还是绝对路径。使用makeAbsolute()来转换相对路径的QDir转换成绝对路径的QDir.


下面来遍历“G:/” ,并以两种方式输出目录

(一)测试代码

#include <QtCore/QCoreApplication>
#include <QDir>
#include <QList>
#include <QFileInfoList>
#include <QDebug>
#include <QTextCodec>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForCStrings(codec);

    QDir d("G:/");
    d.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks | QDir::AllDirs);//列出文件,列出隐藏文件(在Unix下就是以.开始的为文件),不列出符号链接(不支持符号连接的操作系统会忽略)
    d.setSorting(QDir::Size | QDir::Reversed);//按文件大小排序,相反的排序顺序
    const QFileInfoList list = d.entryInfoList();//返回这个目录中所有目录和文件的QFileInfo对象的列表
    QFileInfoList::const_iterator iterator = list.begin();
    qDebug() << "目录和文件的数量: " << d.count();//返回找到的目录和文件的数量
    qDebug() << "fileName                tsize";
    while(iterator != list.end()){
        qDebug() << (*iterator).fileName()<<"             "<<(*iterator).size();
        iterator++;
    }
    qDebug() << "当前目录: " << d.current();//返回应用程序当前目录。
    qDebug() << "当前目录的绝对路径" << d.currentPath();//返回应用程序当前目录的绝对路径。
    const QList<QString> list1 = d.entryList(); //返回这个目录中所有目录和文件的名称的列表
    QList<QString>::const_iterator iterator1 = list1.begin();
    while(iterator1 != list1.end()){
        qDebug()<< (*iterator1);
        iterator1++;
    }

     return a.exec();
}


(二)测试结果

QT输入输出(三)之 目录遍历(QDir)

(三)总结

1、QDir::entryList()会获取该目录下所有的子目录

const QList<QString> list1 = d.entryList(); //返回这个目录中所有目录和文件的名称的列表

注意要用QLis<QString>类型接收。

2、QDir::entryInfoList()会获取该目录下所有目录和文件的QFileInfo对象的列表

const QFileInfoList list = d.entryInfoList();    //返回这个目录中所有目录和文件的QFileInfo对象的列表

同样注意接收变量的类型  QFileInfoList,该类型是QFileInfo的list。

The QFileInfo class provides system-independent file information.

类UNIX系统里:

#ifdef Q_OS_UNIX

 QFileInfo info1("/home/bob/bin/untabify");
 info1.isSymLink();          // returns true
 info1.absoluteFilePath();   // returns "/home/bob/bin/untabify"
 info1.size();               // returns 56201
 info1.symLinkTarget();      // returns "/opt/pretty++/bin/untabify"

 QFileInfo info2(info1.symLinkTarget());
 info2.isSymLink();          // returns false
 info2.absoluteFilePath();   // returns "/opt/pretty++/bin/untabify"
 info2.size();               // returns 56201

 #endif

windows下:

 #ifdef Q_OS_WIN

 QFileInfo info1("C:\\Documents and Settings\\Bob\\untabify.lnk");
 info1.isSymLink();          // returns true
 info1.absoluteFilePath();   // returns "C:/Documents and Settings/Bob/untabify.lnk"
 info1.size();               // returns 743
 info1.symLinkTarget();      // returns "C:/Pretty++/untabify"

 QFileInfo info2(info1.symLinkTarget());
 info2.isSymLink();          // returns false
 info2.absoluteFilePath();   // returns "C:/Pretty++/untabify"
 info2.size();               // returns 63942

 #endif
3、设置文件滤波器

QChar QDir::separator () [static]
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\" under Windows.

You do not need to use this function to build file paths. If you always use "/", Qt will translate your paths to conform to the underlying operating system. If you want to display paths to the user using their operating system's separator use toNativeSeparators().

bool QDir::setCurrent ( const QString & path ) [static]
Sets the application's current working directory to path. Returns true if the directory was successfully changed; otherwise returns false.

See also current(), currentPath(), home(), root(), and temp().

void QDir::setFilter ( Filters filters )
Sets the filter used by entryList() and entryInfoList() to filters. The filter is used to specify the kind of files that should be returned by entryList() and entryInfoList(). See QDir::Filter.

See also filter() and setNameFilters().

void QDir::setNameFilters ( const QStringList & nameFilters )
Sets the name filters used by entryList() and entryInfoList() to the list of filters specified by nameFilters.

Each name filter is a wildcard (globbing) filter that understands * and ? wildcards. (See QRegExp wildcard matching.)

For example, the following code sets three name filters on a QDir to ensure that only files with extensions typically used for C++ source files are listed:

     QStringList filters;
     filters << "*.cpp" << "*.cxx" << "*.cc";
     dir.setNameFilters(filters);