QT目录遍历(QDir)

155 篇文章 26 订阅

QT目录遍历(QDir)

  1. QDir类提供了访问目录结构和他们的内容的方法。
  2. QDir类提供了访问目录结构和它们的内容的与平台无关的方式。
  3. QDir用来操作路径名称、关于路径和文件的访问信息和操作底层文件系统。
  4. 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();

}

测试结果

https://i-blog.csdnimg.cn/blog_migrate/6141b4a17746dfe530399df24a5b8cdb.png

总结

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

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

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

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

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

同样注意接收变量的类型  QFileInfoList,该类型是QFileInfolist

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);

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值