Results 1 to 3 of 3

Thread: Listing files by filter on Macbook

  1. #1
    Join Date
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X

    Default 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.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default 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:
    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:
    Qt Code:
    1. QDir dir("/Users/darnold/Documents/temp/myQt/zetcode/Filters");
    To copy to clipboard, switch view to plain text mode 

    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

  3. #3
    Join Date
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Listing files by filter on Macbook

    Well done! That worked perfectly.

    Thanks.

    D.

Similar Threads

  1. QDirModel (filter files, custom Icon, look)
    By prashant in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2009, 09:39
  2. listing derectory contents
    By ramamurthy.kv in forum Qt Programming
    Replies: 9
    Last Post: 5th May 2008, 19:59
  3. listing the files that has been uploaded to postgres
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 10th September 2007, 10:43
  4. class functions are not listing [ in VC++ editor]
    By joseph in forum Qt Programming
    Replies: 8
    Last Post: 4th April 2007, 11:43
  5. Qt4: QDir::entryList bug in windows when listing network files?
    By Yvon Halbwachs in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 14:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.