Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Filter filename in QDialog
Forum Updated to NodeBB v4.3 + New Features

Filter filename in QDialog

Scheduled Pinned Locked Moved General and Desktop
14 Posts 5 Posters 6.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    broadpeak
    wrote on last edited by
    #2

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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gRicky
      wrote on last edited by
      #3

      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.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Peggy
        wrote on last edited by
        #4

        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:(

        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on last edited by
          #5

          [quote author="Peggy" date="1350649293"]
          dialog.setFilters(lst);
          [/quote]

          I think, setNameFilters will be your friend (and not the setFilters!)

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gRicky
            wrote on last edited by
            #6

            I think it too.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Peggy
              wrote on last edited by
              #7

              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.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KA51O
                wrote on last edited by
                #8

                How about you show us your new code?

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Peggy
                  wrote on last edited by
                  #9

                  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.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KA51O
                    wrote on last edited by
                    #10

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

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Peggy
                      wrote on last edited by
                      #11

                      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:)

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        Peggy
                        wrote on last edited by
                        #12

                        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.

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          butterface
                          wrote on last edited by
                          #13

                          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();
                          @

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            butterface
                            wrote on last edited by
                            #14

                            AFAIK and without RTFM the name of the actual file (project1.cst) or the pattern (*.exe) must be enclosed by brackets.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved