File type filter not working with QFileDialog



  • Hello Everyone,
    I am new to Qt and mostly use PyQt5 but i decided to learn Qt, and as a start point i read few online tutorials and practiced few things.
    Now I am working on a very small project, in which i just have to save the path of file selected, and there are only few file types allowed i.e. *.srec, *.hex, *.mot.
    But unfortunately, I am not able to use file type using QFileDialog in Qt, but the same thing is working in PyQt5.

    The piece code of code i am using is as follow:

    void Flasher::on_actionOpen_triggered()
    {
      QString filepath = QFileDialog::getOpenFileName(
            this,
            tr("Open File"),
            "srec file(*.srec) ;; hex file(*.hex) ;; mot file(*.mot)"
            );
      //QString filename = filepath.right()
      ui->lbl_filepath->setText(filepath);
      fileloaded = true;
    }
    

    and the output it is producing is as follow.
    0_1509298695018_NotWorking.PNG

    While the similar code in Python and PyQt5, I am getting results.

        filetype = 'srec file(*.srec) ;; hex file(*.hex) ;; ihex file(*.ihex) ;; motorola file(*.mot)'
        filepath, _ = QFileDialog.getOpenFileName(Flasher, \
                                                  'Open File for Programming',\
                                                  filter=filetype,\
                                                  options=QFileDialog.DontUseNativeDialog)
    

    And output with this is as follow:
    0_1509299126319_Working.png

    Can anyone please help me, what I am doing wrong which is causing this behavior.
    Thanks in Advance.


  • Qt Champions 2016

    Try like this:

    "srec file (*.srec);;hex file (*.hex);;mot file (*.mot)"
    


  • @kshegunov said in File type filter not working with QFileDialog:

    "srec file (.srec);;hex file (.hex);;mot file (*.mot)"

    Still same problem.

    void Flasher::on_actionOpen_triggered()
    {
      // QString selfilter = tr("srec file(*.srec) ");
      QString filepath = QFileDialog::getOpenFileName(
            this,
            tr("Open File"),
            "srec file (*.srec);;hex file (*.hex);;mot file (*.mot)"
            );
      //QString filename = filepath.right()
      ui->lbl_filepath->setText(filepath);
      fileloaded = true;
    }
    

    0_1509347400327_Problem.PNG



  • File type filter is coming on file name, so I added one empty string and it started working properly.
    The updated code is as follow:

    void Flasher::on_actionOpen_triggered()
    {
      // QString selfilter = tr("srec file(*.srec) ");
      QString filepath = QFileDialog::getOpenFileName(
            this,
            tr("Open File"),
            "",
            "srec file (*.srec);;hex file (*.hex);;mot file (*.mot)"
            );
      // QString filename = filepath.right()
      ui->lbl_filepath->setText(filepath);
      fileloaded = true;
    }
    

    This works for me.


Log in to reply
 

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