Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileDialog setNameFilter can be fooled
Qt 6.12 LTS is out! See what's new in the release blog

QFileDialog setNameFilter can be fooled

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.1k 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.
  • A Offline
    A Offline
    Amomum
    wrote on last edited by
    #1

    Let's say I want a QFileDialog that can open only specific extensions. I do this:

    m_dialog.setFileMode(QFileDialog::ExistingFile);
    m_dialog.setOption(QFileDialog::ReadOnly, true);
    
    m_dialog.setNameFilter(tr("Models (*.jpg)"));
    
    m_dialog.setOption(QFileDialog::ReadOnly, true);
    m_dialog.setViewMode(QFileDialog::List);
    m_dialog.setAcceptMode(QFileDialog::AcceptOpen);
    

    Unfortunately, I still can write a filename by hand in the textbox and it will be opened even if its extension is not listed in the setNameFilter arguments.

    I thought that maybe that's the 'feature' of the windows native window, so I set

    m_dialog.setOption( QFileDialog::DontUseNativeDialog);
    

    and 'Open' button in the dialog became disabled. Unfortunately, writing filename by hand and pressing 'Enter' still opens incorrect file.

    So I have to filter by extension twice.

    I just want to know, if that behaviour is normal or if it's a bug in Qt.

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What version of Qt are you using on which platform ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What version of Qt are you using on which platform ?

        A Offline
        A Offline
        Amomum
        wrote on last edited by
        #3

        @SGaist I'm using QT 5.7.1 on Windows.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you validate that with a more recent version of Qt ?

          Note that I suspect that it's a Windows specific behaviour. By default Qt uses the native file dialog which may have some quirks.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • A Amomum

            Let's say I want a QFileDialog that can open only specific extensions. I do this:

            m_dialog.setFileMode(QFileDialog::ExistingFile);
            m_dialog.setOption(QFileDialog::ReadOnly, true);
            
            m_dialog.setNameFilter(tr("Models (*.jpg)"));
            
            m_dialog.setOption(QFileDialog::ReadOnly, true);
            m_dialog.setViewMode(QFileDialog::List);
            m_dialog.setAcceptMode(QFileDialog::AcceptOpen);
            

            Unfortunately, I still can write a filename by hand in the textbox and it will be opened even if its extension is not listed in the setNameFilter arguments.

            I thought that maybe that's the 'feature' of the windows native window, so I set

            m_dialog.setOption( QFileDialog::DontUseNativeDialog);
            

            and 'Open' button in the dialog became disabled. Unfortunately, writing filename by hand and pressing 'Enter' still opens incorrect file.

            So I have to filter by extension twice.

            I just want to know, if that behaviour is normal or if it's a bug in Qt.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Amomum
            You want the filter to actually prevent a user from being able to specify a filename with a certain extension. But I don't think "filters" have ever claimed to do that. From the docs:

            In the above example, the filter is set to "Images (*.png *.xpm *.jpg)", this means that only files with the extension png, xpm, or jpg will be shown in the QFileDialog.

            I think they only determine what filenames are shown for selection. If the user types something, I don't they have anything to do with validating the response.

            So, yes, for safety you have to validate the final result yourself if it matters.

            1 Reply Last reply
            2

            • Login

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