简体   繁体   English

是否存在在 EntryList() 中使用过滤器的更简单方法?

[英]Does simplier way to use filters in EntryList() exist?

I am new in Qt.我是 Qt 的新手。 I have two types of files in my directory.我的目录中有两种类型的文件。 First I need to work with one type and then with another.首先我需要使用一种类型,然后使用另一种类型。 I decided to use EntryList() with name filters like " .png" and " .txt" and it works pretty well.我决定将 EntryList() 与诸如“ .png”和“ .txt”之类的名称过滤器一起使用,并且效果很好。

But this method requiers filters with QStringList() type as an input.但是此方法需要使用 QStringList() 类型作为输入的过滤器。 So I wonder can I do it simplier way because I will not use this filters more than once and so I dont want to keep another Lists in my memory.所以我想知道我能不能用更简单的方式来做,因为我不会多次使用这个过滤器,所以我不想在我的记忆中保留另一个列表。

How I do this now:我现在如何做到这一点:

QStringList png_filter("*.png");
QStringList frst_filter = Dir.entryList(png_filter);
QStringList txt_filter("*.txt");
QStringList scnd_filter = Dir.entryList(txt_filter);
cout<<frst_filter.size()<<"  "<<scnd_filter.size()<<endl;

Or:或者:

QStringList filter;
filter.push_back("*.png");
frst_filter = Dir.entryList(filter);
filter.pop_back();
filter.push_back("*.txt");
scnd_filter = Dir.entryList(filter);
cout<<frst_filter.size()<<"  "<<scnd_filter.size()<<endl;

PS Found no useful info here: https://doc.qt.io/qt-5/qdir.html#entryList PS在这里找不到有用的信息: https : //doc.qt.io/qt-5/qdir.html#entryList

Perhaps i do not understand the problem.也许我不明白这个问题。 If you create the QStringList on the stack, it will be destroyed after leaving the function.如果在栈上创建了QStringList,离开函数后就会被销毁。

So here is a shorter version:所以这是一个较短的版本:

Dir.enryList(QStringList() << "*.png" << "*.txt");

If you use C++11 or greater you can use initializer lists:如果您使用 C++11 或更高版本,则可以使用初始化列表:

Dir.enryList({"*.png", "*.txt"});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM