c++qtqdirqstringlist

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


I am new in 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.

But this method requiers filters with QStringList() type as an input. 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;

P.S. Found no useful info here: https://doc.qt.io/qt-5/qdir.html#entryList


Solution

  • Perhaps i do not understand the problem. If you create the QStringList on the stack, it will be destroyed after leaving the function.

    So here is a shorter version:

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

    If you use C++11 or greater you can use initializer lists:

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