Filter filename in QDialog
-
Hello broadpeak,
I tried it but it does not work :( For example I have 4 files which have been saved in temp directory and just the name of two of them in the database (test_pro1,test_pro2). The code is as following:
@
QFileDialog dialog(this);
QStringList lst;
lst<<"test_pro1"<<"test_pro2";
dialog.setFilters(lst);
dialog.getOpenFileName(this,tr("Open the files"),QDir("temp").absolutePath());
@I currently read that by overriding proxymodel on QDir, the filtering would be possible. But it is not also exactly what I am looking for:(
-
[quote author="Peggy" date="1350649293"]
dialog.setFilters(lst);
[/quote]I think, setNameFilters will be your friend (and not the setFilters!)
-
I think it too.
-
I tried setNameFilters and the tip from gRicky. It does not work as I want. It showed all the files in the directory which I don't want.
-
How about you show us your new code?
-
Here is my code:
@
QFileDialog* dialog = new QFileDialog;
QStringList dbLst;
dbLst<<"project1"<<"project2";
for (QStringList::iterator it = dbLst.begin();it != dbLst.end(); it++){
QString str = it;
}
dialog->setFilters(QStringList()<< ".cst");
dialog->setDirectory(QDir("Template").absolutePath());
dialog>exec();@
I used the Iterator to see the content of my list,which of course is in this example is not useful.
-
@
QFileDialog* dialog = new QFileDialog;
QStringList dbLst;
dbLst<<"project1"<<"project2";// whats this for-loop for ?
for (QStringList::iterator it = dbLst.begin();it != dbLst.end(); it++){ QString str = *it; }
//you are not using setNameFilters and also you are handing over a stringlist containing only ".cst" as the parameter. I thought you were looking for names containing "project1" or "project2".
dialog->setFilters(QStringList()<< ".cst");
dialog->setDirectory(QDir("Template").absolutePath());
dialog>exec();@
Please have a look at "the documentation":http://qt-project.org/doc/qt-4.8/qfiledialog.html#setNameFilters and think about want you want to achieve before wildly copying code together.
-
I think what I am looking for is hardly being solved by setting filter(at least I can not solve it with filter). I will write an extra class with inherits QDialog and try to write some methods to solve my problem.
Thank you all for taking your time to help me:)
-
Hello KA510,
You are right with *.cst(my fault). But it does not filter the name I am looking for it either. I may understand the filter in qt not correctly. By clicking on open menu, I was looking for to have a list of files which were filtered according to the existing list in database. So if I have 4 files in the directory which only two of them existed in database I wanted to show just this two files except the 4 files.
-
Try this one
@
QFileDialog* dialog = new QFileDialog;
QStringList dbLst;
dbLst<<"(project1.cst)"<<"(project2.cst)";
dialog->setFilters(dbLst);
dialog->setDirectory(QDir("Template").absolutePath());
dialog->exec();
@EDIT
or maybe you need this
@
QFileDialog* dialog = new QFileDialog;
dialog->setNameFilter(QString("My Files (project1.cst project2.cst)"));
dialog->setDirectory(QDir("Template").absolutePath());
dialog->exec();
@ -
AFAIK and without RTFM the name of the actual file (project1.cst) or the pattern (*.exe) must be enclosed by brackets.