What's new in QML tooling in Qt 6.11, part 2: new qmllint warnings
Contents
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, and part 3 on the context property support 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:
virtualfor properties that can be shadowed.overridefor properties that must shadow another property.finalfor 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:
- A property incorrectly overrides another one. ([property-override])
- Multiple inline components have the same name. ([duplicate-inline-components])
- Multiple members defined in the same type have the same name. ([duplicated-name])
- An id shadows another member if the member is in use. ([id-shadows-member]*)
- A member shadows another one. ([shadow]**)
(*) 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:
vardeclarations in JavaScript blocks. ([var-used-in-block-scope])- Comma expression usage. ([comma])
- Expression statements that do nothing. ([confusing-expression-statement])
- Expressions like
---xor+++x. ([confusing-minuses] and [confusing-pluses]) voidoperator andeval()function usage. ([void] and [eval])==operator usage with value coercion. ([equality-type-coercion])- Usage of
?.on elements that can’t be null. ([redundant-optional-chaining]) - Usage of empty JavaScript blocks in bindings that don’t return any value. ([unintentional-empty-block])
- Code that never runs. ([unreachable-code])
- Case blocks not ending with
return,break,throw, orcontinue. ([unterminated-case]) - Usage of variables before their definition. ([var-used-before-declaration])
- Usage of assignment operators in conditionals. ([assignment-in-condition])
Withstatement usage. ([with])- Usage of literal constructors, like
new Boolean(). ([literal-constructor]) - Usage of functions before their definition. ([function-used-before-declaration]*)
(*) This warning is not enabled by default.
New enumerations warnings
We added multiple warnings on the usage and definition of enumerations in QML:
- Incorrect usage of
enumas a type name. ([enums-are-not-types]) - Enumerations defined at the wrong places. ([non-root-enums] and [inline-component-enums])
- Usage of non-existing enumeration entries. ([missing-enum-entry])
- Lowercase enumerations. ([enum-key-case])
- Enumerations with name collisions. ([enum-entry-matches-enum])
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:
- Assigning multiple times to the same non-list property. ([non-list-property])
- Calling a property as if it were a function. ([use-proper-function])
- Preferring specific types instead of var in property definitions. ([prefer-non-var-properties])
- Accessing a singleton instead of an attached property. ([access-singleton-via-object])
- Forgetting to set a required property. ([required])
- Binding more or fewer than one child in a Component. ([component-children-count])
- Forgetting to make a non-constant C++ property notifiable. ([stale-property-read])
New miscellaneous warnings
Last but not least, we also added warnings that didn’t relate to the previous categories:
- Using invalid qmllint comment directives. ([invalid-lint-directive])
- Using Component as a top-level type in a QML file. ([top-level-component])
- Instantiating uncreatable types. ([uncreatable-type])
- Importing QML modules using file selectors. ([import-file-selector]*)
(*) 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.
Part 3 on context properties can be found here.