PDA

View Full Version : QDirModel with QSortFilterProxyModel and regexp



mchrk
14th January 2009, 11:50
Hello,

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



QDirModel dirModel;
QTreeView tree;
QSortFilterProxyModel proxyModel;

dirModel.setFilter( QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot );

proxyModel.setSourceModel(&dirModel);
tree.setModel(&proxyModel);

QModelIndex idx = proxyModel.mapFromSource(dirModel.index("./") );
tree.setRootIndex(idx);

proxyModel.setFilterRegExp(QRegExp(".cpp", Qt::CaseInsensitive, QRegExp::FixedString));
proxyModel.setFilterKeyColumn(0);

tree.setAnimated(false);
tree.setIndentation(20);
tree.setSortingEnabled(true);

tree.setWindowTitle(QObject::tr("Dir View"));
tree.resize(640, 480);
tree.show();

caduel
15th January 2009, 08:41
assuming you want all files with the extension .cpp that would be one of

QRegExp("*.cpp", Qt::CaseInsensitive, QRegExp::WildCard)
QRegExp("\\.*.cpp", Qt::CaseInsensitive)

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

HTH

wysota
15th January 2009, 09:12
...or QRegExp(".*\\.cpp").

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

mchrk
15th January 2009, 09:26
assuming you want all files with the extension .cpp that would be one of

QRegExp("*.cpp", Qt::CaseInsensitive, QRegExp::WildCard)
QRegExp("\\.*.cpp", Qt::CaseInsensitive)

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.

mchrk
15th January 2009, 09:27
...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).

caduel
15th January 2009, 09:34
@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?

mchrk
15th January 2009, 12:13
@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.