How to show folder with certain file name in it
-
see this, setfilter controls only the file type
@
void QFileDialog::setFilter ( QDir::Filters filters )
Sets the filter used by the model to filters. The filter is used to specify the kind of files that should be shown.This function was introduced in Qt 4.4.
@ -
we are 1 step forward, i use this in the mycalss.cpp
@
QStringList files = dir.entryList(QStringList("abc.txt"),
QDir::Files | QDir::NoSymLinks);qDebug () << files << files.contains("abc.txt");
@
now I get in the debug when I click on browse@
() false
("abc.txt") true
() false
@but it ignores my if condition simply and shows me all the files ...
any idea? may be i ve a mistake in the call?
-
[quote author="SherifOmran" date="1344933412"]we are 1 step forward, i use this in the mycalss.cpp
but it ignores my if condition simply and shows me all the files ...
[/quote]
which if condition? -
class
@
bool myclass::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
{QModelIndex index = sourceModel()->index(source_row, 0/*column*/, source_parent); QFileSystemModel* filemodel = qobject_cast<QFileSystemModel*>(sourceModel()); // QFileSystemModel *filemodel = static_cast<QFileSystemModel*>(sourceModel()); filemodel->setRootPath(QDir::currentPath()); QString fullPath = filemodel->fileInfo(index).absoluteFilePath(); QDir dir(fullPath); QStringList files = dir.entryList(QStringList("abc.txt"), QDir::Files | QDir::NoSymLinks); qDebug () << files << files.contains("abc.txt");
if (files.contains("abc.txt")) return true; else false;
}
@
-
so you need to change the filterAcceptsRow function to be as:
if the index represent a folder return true
else if its a file and his name == to "abc.txt" return true
else return false
in that case you will get all folder (as empty) and you'll see only the file with name "abc.txt"@
bool myclass::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
{
QModelIndex index = sourceModel()->index(source_row, 0/column/, source_parent);
QFileSystemModel* filemodel = qobject_cast<QFileSystemModel*>(sourceModel());
QFileInfo indexInfo = filemodel->fileInfo(index);
if( indexInfo.isDir())
return true;
else if (indexInfo.isFile() && indexInfo.fileName() == QString("abc.txt"))
return true;
else return false;
}
@ -
It does not work.
I use this as the calling function, is it correct?
@
QFileDialog *fileDialog = new QFileDialog;
myclass *sourceModel = new myclass;
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(sourceModel);
fileDialog->setProxyModel(proxyModel);
fileDialog->show();
@ -
please see this link, it seems there is a bug
https://bugreports.qt-project.org/browse/QTBUG-7739 -
the bug report refer to code like this:
@
QFileDialog fileDialog;
fileDialog.setFilter("abc.txt");
fileDialog.exec();
@and not for the code using QSortFilterProxyModel.
about your code:
[quote author="SherifOmran" date="1344951506"]It does not work.I use this as the calling function, is it correct?
@
QFileDialog *fileDialog = new QFileDialog;
myclass *sourceModel = new myclass;
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(sourceModel);
fileDialog->setProxyModel(proxyModel);
fileDialog->show();
@
.
[/quote]you use it in the wrong way instead use it as follow
@
QFileDialog *fileDialog = new QFileDialog;
//you implemented MyClass as proxy model so you need to set it in QFileDialog as proxyModel
myclass *proxyModel = new myclass;
fileDialog->setProxyModel(proxyModel);
fileDialog->show();
@and use the filterAcceptRow as before
[quote author="lahianim" date="1344944559"]so you need to change the filterAcceptsRow function to be as:
if the index represent a folder return true
else if its a file and his name == to "abc.txt" return true
else return false
in that case you will get all folder (as empty) and you'll see only the file with name "abc.txt"@
bool myclass::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
{
QModelIndex index = sourceModel()->index(source_row, 0/column/, source_parent);
QFileSystemModel* filemodel = qobject_cast<QFileSystemModel*>(sourceModel());
QFileInfo indexInfo = filemodel->fileInfo(index);
if( indexInfo.isDir())
return true;
else if (indexInfo.isFile() && indexInfo.fileName() == QString("abc.txt"))
return true;
else return false;
}
@[/quote] -
even this, it does not work my friend on MAC. It seems to be a bug.
-