Results 1 to 7 of 7

Thread: QDirModel with QSortFilterProxyModel and regexp

  1. #1
    Join Date
    May 2008
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QDirModel with QSortFilterProxyModel and regexp

    Hello,

    I'm trying to create a QDirModel that only shows .cpp files but it doesn't display anything.
    What am I doing wrong?

    Qt Code:
    1. QDirModel dirModel;
    2. QTreeView tree;
    3.  
    4. dirModel.setFilter( QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot );
    5.  
    6. proxyModel.setSourceModel(&dirModel);
    7. tree.setModel(&proxyModel);
    8.  
    9. QModelIndex idx = proxyModel.mapFromSource(dirModel.index("./") );
    10. tree.setRootIndex(idx);
    11.  
    12. proxyModel.setFilterRegExp(QRegExp(".cpp", Qt::CaseInsensitive, QRegExp::FixedString));
    13. proxyModel.setFilterKeyColumn(0);
    14.  
    15. tree.setAnimated(false);
    16. tree.setIndentation(20);
    17. tree.setSortingEnabled(true);
    18.  
    19. tree.setWindowTitle(QObject::tr("Dir View"));
    20. tree.resize(640, 480);
    21. tree.show();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDirModel with QSortFilterProxyModel and regexp

    assuming you want all files with the extension .cpp that would be one of
    Qt Code:
    1. QRegExp("*.cpp", Qt::CaseInsensitive, QRegExp::WildCard)
    2. QRegExp("\\.*.cpp", Qt::CaseInsensitive)
    To copy to clipboard, switch view to plain text mode 

    Your regexp only matches files named ".cpp" without anything in front of it.

    HTH

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDirModel with QSortFilterProxyModel and regexp

    ...or QRegExp(".*\\.cpp").

    I'm not sure about mapping the slashdot like that, by the way.

  4. #4
    Join Date
    May 2008
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QDirModel with QSortFilterProxyModel and regexp

    Quote Originally Posted by caduel View Post
    assuming you want all files with the extension .cpp that would be one of
    Qt Code:
    1. QRegExp("*.cpp", Qt::CaseInsensitive, QRegExp::WildCard)
    2. QRegExp("\\.*.cpp", Qt::CaseInsensitive)
    To copy to clipboard, switch view to plain text mode 

    Your regexp only matches files named ".cpp" without anything in front of it.

    HTH
    Sorry, forgott to mention that I tried your approach to, this does not work, I just get a empty list.

  5. #5
    Join Date
    May 2008
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QDirModel with QSortFilterProxyModel and regexp

    Quote Originally Posted by wysota View Post
    ...or QRegExp(".*\\.cpp").

    I'm not sure about mapping the slashdot like that, by the way.
    How should I map it any other way? I tried to map it to "/Users/mchrk/code/foo/src/" and I got the same result as before (nothing).
    Last edited by mchrk; 15th January 2009 at 08:51.

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDirModel with QSortFilterProxyModel and regexp

    @Wysota: oops... that was wrong of course. the \\ indeed has to be in front of the second .
    @mchrk: have you checked if it works without setting a rootIndex?
    have you checked that proxyModel.mapFromSource(dirModel.index("./") ) yields a valid QModelIndex?

  7. #7
    Join Date
    May 2008
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QDirModel with QSortFilterProxyModel and regexp

    Quote Originally Posted by caduel View Post
    @Wysota: oops... that was wrong of course. the \\ indeed has to be in front of the second .
    @mchrk: have you checked if it works without setting a rootIndex?
    have you checked that proxyModel.mapFromSource(dirModel.index("./") ) yields a valid QModelIndex?
    Yes I checked the model index with idx.isValid() and it returns true.

Similar Threads

  1. Simple RegExp Question...
    By jared.carlson23 in forum Newbie
    Replies: 1
    Last Post: 4th July 2008, 14:10
  2. IPAddress Validator RegExp
    By user_mail07 in forum Qt Programming
    Replies: 0
    Last Post: 3rd April 2008, 02:36
  3. RegExp question
    By high_flyer in forum General Programming
    Replies: 1
    Last Post: 26th August 2007, 18:23
  4. QStringList.filter using RegExp
    By grellsworth in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 14:09
  5. Quick RegExp question
    By stealth86 in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2007, 08:23

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
  •  
Qt is a trademark of The Qt Company.