Listing files by filter on Macbook
All,
Running the following file in Qt Creator on a Macbook Pro 19.6.2.
#include <QTextStream>
#include <QDir>
int main()
{
QTextStream out(stdout);
QDir dir;
QStringList filters;
filters << "*.cpp" << "*.o";
dir.setNameFilters(filters);
QFileInfoList list = dir.entryInfoList();
for (int i=0; i<list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
out << QString("%1").arg(fileInfo.fileName());
out << endl;
}
}
The directory contains some .cpp and .o files,
david-arnolds-macbook-pro-2:Filters darnold$ pwd
/Users/darnold/Documents/temp/myQt/zetcode/Filters
david-arnolds-macbook-pro-2:Filters darnold$ ls -ll
total 456
drwxr-xr-x 3 darnold staff 102 Mar 29 16:52 Filters.app
-rw-r--r-- 1 darnold staff 23 Mar 29 16:44 Filters.pro
-rw-r--r-- 1 darnold staff 7820 Mar 29 17:04 Filters.pro.user
-rw-r--r-- 1 darnold staff 9213 Mar 29 16:52 Makefile
-rw-r--r-- 1 darnold staff 404 Mar 29 17:21 filters.cpp
-rw-r--r-- 1 darnold staff 204600 Mar 29 17:22 filters.o
However, when I run Fileters.app from Qt Creator, here is the output:
Starting /Users/darnold/Documents/temp/myQt/zetcode/Filters/Filters.app/Contents/MacOS/Filters...
/Users/darnold/Documents/temp/myQt/zetcode/Filters/Filters.app/Contents/MacOS/Filters exited with code 0
Now (remember I'm on a Mac) if I "Show Package Contents" for Fileters.app, and drop the files filters.cpp and filters.o in /Users/darnold/Documents/temp/myQt/zetcode/Filters/Filters.app/Contents/MacOS/Filters, then "Run" the file again from QtCreator, I get:
/Users/darnold/Documents/temp/myQt/zetcode/Filters/Filters.app/Contents/MacOS/Filters...
filters.cpp
filters.o
/Users/darnold/Documents/temp/myQt/zetcode/Filters/Filters.app/Contents/MacOS/Filters exited with code 0
Should I be constructing my code differently if I want to view the contents of the directory /Users/darnold/Documents/temp/myQt/zetcode/Filters/?
David.
Re: Listing files by filter on Macbook
Your code works fine on my Linux box but my executable is in the same directory as the source and build files. Don't know why you have your executable in a separate directory but that looks like the problem. From the Qt docs:
Quote:
QDir::QDir ( const QString & path = QString() )
Constructs a QDir pointing to the given directory path. If path is empty the program's working directory, ("."), is used.
So you get a listing of "/Users/darnold/Documents/temp/myQt/zetcode/Filters/Filters.app/Contents/MacOS/Filters". If you want a listing of the directory that contains your source code you need to use this:
Code:
QDir dir
("/Users/darnold/Documents/temp/myQt/zetcode/Filters");
That might be the problem you describe in your "Input a text file" post, too. The executable is going to look for the text file in the directory where it resides.
HTH
Re: Listing files by filter on Macbook
Well done! That worked perfectly.
Thanks.
D.