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

qmllint: access properties outside delegate without qmllint warnings

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P2: Important
    • Resolution: Done
    • Affects Version/s: 6.3.0
    • Fix Version/s: 6.4.0 Beta1, 6.4
    • Component/s: QML: Tooling
    • Labels:
    • Commits:
      4d71091a19 (qt/qtdeclarative/dev) 4d71091a19 (qt/tqtc-qtdeclarative/dev)

      Description

      Accessing properties outside of a delegate without producing qmllint warnings seems to be harder than it should be. The 3th example below which does not produce any warnings is not intuitive. I had to look to the bug reports to find it. I would suspect the 2nd example to be accepted by qmllint since this is the documented way of accessing properties outside of a delegate.

      This is a common pattern in our codebase and since there are already two related bugs, I assume many users will encounter this issue. If this isn't easily fixed, documenting this somewhere would be useful.

      Example 1

      The first example gives an unqualified access warning since ids can't be referenced outside of a component (see QTBUG-101012 & QTBUG-101867). I agree that this is not a bug.

      import QtQuick
      Window {
          visible: true
          ListView {
              id: listView
              anchors.fill: parent
              model: 1
              property string foo: "foo"
              delegate: Text {
                  text: listView.foo
              }
          }
      }
      

      produces:

      Warning: main.qml:10:19: Unqualified access
                  text: listView.foo
                        ^^^^^^^^
      

      Example 2

      The second example uses ListView.view to access the property as described in the documentation (https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#accessing-views-and-models-from-delegates). However, this produces another qmllint warning.

      import QtQuick
      Window {
          visible: true
          ListView {
              anchors.fill: parent
              model: 1
              property string foo: "foo"
              delegate: Text {
                  text: ListView.view.foo
              }
          }
      }
      

      produces:

      Warning: main.qml:9:33: Property "foo" not found on type "QQuickItemView"
                  text: ListView.view.foo
                                      ^^^
      

      Example 3

      The last example uses a workaround taken from the comments of QTBUG-101867. While this does not produce any qmllint warnings, it requires an explicit conversion to var. It would be nice if qmllint could handle this implicitly.

      import QtQuick
      Window {
          visible: true
          ListView {
              anchors.fill: parent
              model: 1
              property string foo: "foo"
              delegate: Text {
                  property var listView: ListView.view
                  text: listView.foo
              }
          }
      }
      

        Attachments

          Issue Links

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

            Activity

              People

              Assignee:
              ulherman Ulf Hermann
              Reporter:
              timvdm Tim Vandermeersch
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:

                  Gerrit Reviews

                  There are no open Gerrit changes