Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-120358

FileDialog from Qt.labs.platform module cannot update selectedNameFilter.index correctly

    XMLWordPrintable

Details

    Description

      A simple reproducer:

      ApplicationWindow {
          visible: true
          width: 400
          height: 200
      
          Button {
              text: "Open"
              onClicked: dialog.open()
          }
      
          Platform.FileDialog {
              id: dialog
              title: "title"
              fileMode: Platform.FileDialog.SaveFile
              nameFilters: ["csv file" + " (*.csv)",
              "text file" + " (*.txt)"]
              onAccepted: {
                  var n = selectedNameFilter.name
                  var i = selectedNameFilter.index
                  console.log(`Name: ${n}, Index: ${i}`)
              }
              // selectedNameFilter.onNameChanged: {
              // }
              // selectedNameFilter.onIndexChanged: {
              // }
          }
      }
      

      Run the sample, click button to open FileDialog for the first time, then save file. Even if one selects "save type as" from drop-down, the output to console is "qml: Name: , Index: -1" for the first run. The output is correct from the second run though.

      Current workaround:
      1. Explicitly bind selectedNameFilter.index, as what is done in:
      https://doc.qt.io/qt-6/qml-qtquick-dialogs-filedialog.html#selectedNameFilter-prop
      2. Use FileDialog from QtQuick.Dialog module. The non-lab version behaves correctly.
      3. Uncomment signal handlers, i.e. trying to read selectedNameFilter properties.

      Reason:

      QQuickLabsPlatformFileNameFilter *QQuickLabsPlatformFileDialog::selectedNameFilter()
      

      The function to create the selectednameFilter won't be called unless QML tries to READ m_selectedNameFilter. So for the first run, m_selectedNameFilter does not exist yet. Only after the 1st run there is a valid m_selectedNameFilter.

      Personally speaking, the behavior of non-lab version is preferable.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            vhilshei Volker Hilsheimer
            luqiaochen Luqiao Chen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes