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. File type filter not working with QFileDialog

File type filter not working with QFileDialog

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 5.5k Views 2 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.
  • X Offline
    X Offline
    xpress_embedo
    wrote on last edited by
    #1

    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.

    kshegunovK 1 Reply Last reply
    0
    • X Offline
      X Offline
      xpress_embedo
      wrote on last edited by
      #4

      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.

      1 Reply Last reply
      1
      • X xpress_embedo

        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.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #2

        Try like this:

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

        Read and abide by the Qt Code of Conduct

        X 1 Reply Last reply
        0
        • kshegunovK kshegunov

          Try like this:

          "srec file (*.srec);;hex file (*.hex);;mot file (*.mot)"
          
          X Offline
          X Offline
          xpress_embedo
          wrote on last edited by
          #3

          @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

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xpress_embedo
            wrote on last edited by
            #4

            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.

            1 Reply Last reply
            1

            • Login

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