Details
-
Bug
-
Resolution: Done
-
P2: Important
-
6.3.0
-
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
- relates to
-
QTBUG-101012 qmllint: cannot reference id outside for a Component in the same file
-
- Closed
-
-
QTBUG-101867 qmllint: unqualified access to properties and ids outside the delegate
-
- Closed
-
For Gerrit Dashboard: QTBUG-102806 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
410700,12 | QML: Add an option to bind components to files | dev | qt/qtdeclarative | Status: MERGED | +2 | 0 |
411308,6 | qmlcompiler: Evaluate pragma ComponentBehavior | dev | qt/qtdeclarative | Status: MERGED | +2 | 0 |