The latest Qt release, Qt 6.11, is just around the corner. This short blog post series presents the new features that QML tooling brings in Qt 6.11. You can find part 1 on new qmlls features here.
qmllint got a lot of new features since our last blog post on qmllint warnings, so this blog post will go through the list of qmllint warnings added between Qt 6.6 and Qt 6.11. A list of all existing qmllint warnings, including examples, can be found in the documentation. We will link to the specific warning documentation for each warning listed here so you can easily find more information and examples on how to trigger and fix the warning.
Shadowing QML members
Shadowing happens when your QML component redefines an id, property, signal, or method that already exists in a base class. It is both a powerful and dangerous construct that can lead to weird unexpected behaviors and reduces performance.
In Qt 6.11, we added the virtual and override keywords to mark properties that shadow or can be shadowed. In Qt 6.10, we added the final keyword to mark properties that can't be shadowed. Altogether, we have:
virtual for properties that can be shadowed.
override for properties that must shadow another property.
final for properties that must not be shadowed.
Properties that have neither virtual nor override might shadow silently: no warning would be emitted by the QML runtime or qmllint. Can you spot which property is shadowing in the following code snippet?
// Building.qml
import QtQuick
Item {
property int floors
property string rotation // like CSS rotate, "-120deg"
property date constructionDate
}
Would you have noticed the shadowing property if I hadn't told you there was one? The property rotation already exists in the base type. Code that expects to see the rotation property from the base type will see a string instead and probably won't work with the Building component. To fix this, we could rename the rotation property in Building.qml to cssRotation, for example:
// Building.qml
import QtQuick
Item {
property int floors
property string cssRotation // like CSS rotate, "-120deg"
property date constructionDate
}
Can you guess what the next code snippet outputs?
import QtQuick
Item {
clip: true
Component.onCompleted: console.log(clip) // what does this print?
/* ... */
function clip() { return 123; }
}
This snippet prints out
function clip() { [native code] }
at runtime, instead of an expected true, because the clip method shadows the property of the same name. This example shows that shadowing does not only happen between properties. It can also happen between any combination of properties, signals, methods, and ids. Especially shadowing with ids can easily become confusing, can you guess what the output of this snippet is?
import QtQuick
Item {
x: 42
Component.onCompleted: console.log(x) // what does this print?
/* ... */
Item { id: x }
}
You probably guessed correctly that the output is probably not 42. The lookup to x gives a higher priority to ids than to properties, which makes console.log(x) print out the Item with id x. This is hard to see and debug. Luckily, in Qt 6.12, this won't be a problem anymore with the new [id-shadows-member] qmllint warning.
qmllint will warn for situations where:
(*) This warning will be available starting with Qt 6.12.
(**) To get the full set of shadowing warnings, enable the [shadow] category by adding Shadow=warning inside the .qmllint.ini settings file. The category is disabled by default as not all shadowing cases can currently be fixed. For example, there is currently no way to mark a method as shadowing a property in QML. You can add the // qmllint disable shadow comment to silence the warning for each occurrence.
New JavaScript-specific warnings
We added new JavaScript-specific warnings to qmllint to bring it on par with the embedded Qt Creator QML code model. Those warnings are not really QML-specific, and more about the general usage of JavaScript:
(*) This warning is not enabled by default.
New enumerations warnings
We added multiple warnings on the usage and definition of enumerations in QML:
We removed a warning for unscoped enumerations defined in C++ ([restricted-type]) that warned on enum accesses like ComponentName.EnumName.key. Accessing enums by enum name in QML is allowed in Qt 6.11 and was added by https://codereview.qt-project.org/c/qt/qtdeclarative/+/646544.
New property warnings
We also added some warnings when assigning or binding values to a property:
New miscellaneous warnings
Last but not least, we also added warnings that didn't relate to the previous categories:
(*) This warning is not enabled by default.
Summary
We described the shadowing warning newly introduced in Qt 6.11, with a short outlook to 6.12, and took a look at the long list of newly added warnings since Qt 6.6.
You can find the documentation for all warnings in this list. Feel free to open a bug report in JIRA in case you encounter a warning that is not working as expected or is insufficiently documented.