Filter filename in QDialog



  • Hello everyone,

    I am trying to set a filter on the name of the files which are being shown by clicking on Open menu.
    From one side I saved all the files on a directory. But on the other hand I saved the name of some of them in the Database. Now I want to have a QDialog which shows only the files, that their names existed already in Database. My problem is I want to set a filter that can filter my files. I don't know how I can manage this filter.

    I am looking forward for some tips.



  • Get the filenames from your database into a QStringList.
    And pass this stringlist into this method:
    void QFileDialog::setNameFilters ( const QStringList & filters )



  • How to filter the name in a QFileDialog
    @

        QDate date = QDate::currentDate();
        QString dateString = date.toString("yyyy-MM-dd");
    
        QTime time = QTime::currentTime();
        QString timeString = time.toString();
    
        QFileDialog *fd  = new QFileDialog;
        fd->selectFile&#40;dateString + "_" + timeString + "_log.csv"&#41;; //<- sets the name of the file on the QFile dialog
    
        fd->setFilters(QStringList() << "*.csv"); //<- sets the filter of the extention file
        if (fd->exec&#40;&#41;) //<- show the QFileDialog
            m_logFileName = fd->selectedFiles()[0]; //+ fd->selectedFilter(); // <- gets teh first of the file selected by the user
        qDebug() << m_logFileName; //<- show into the debug window the selected filename
    

    @

    I hope this code help you to understand.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.