Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtqml-tooling-qmllint.qdoc
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtqml-tooling-qmllint.html
6\title qmllint Reference
7\ingroup qtqml-tooling
8\brief A tool for verifying the syntax of QML files and warning about
9anti-patterns.
10
11\e qmllint is a tool shipped with Qt, that verifies the syntatic validity of
12QML files.
13It also warns about some QML anti-patterns. If you want to disable a specific
14warning type, you can find the appropriate flag for doing so by passing
15\c{--help} on the command line.
16
17By default, some issues will result in warnings that will be printed and result
18in a non-zero exit code.
19Minor issues however (such as unused imports) are just informational messages
20by default and will not affect the exit code.
21qmllint is very configurable and allows for disabling warnings or changing how
22they are treated.
23Users may freely turn any issue into a warning, informational message, or
24disable them outright.
25
26qmllint warns about:
27\list
28 \li Unqualified accesses of properties
29 \li Usage of signal handlers without a matching signal
30 \li Usage of with statements in QML
31 \li Issues related to compiling QML code
32 \li Unused imports
33 \li Deprecated components and properties
34 \li And many other things
35\endlist
36
37\note In order for qmllint to work properly, it requires type information.
38That information is provided by QML modules in the import paths.
39The current directory, as well as the import paths for Qt's built-in types,
40are used as import paths by default.
41To add more import paths not included in the default,
42add them via the \c{-I} flag.
43
44To get an overview and explanation of all available command line options, run \c{qmllint --help}.
45
46\section2 Compiler warnings
47
48qmllint can warn you about code that cannot be compiled by \l{qmlsc}.
49
50These warnings are not enabled by default. In order to enable them specify
51\c{--compiler warning} or adjust your settings file accordingly.
52
53\section2 Marking components and properties as deprecated
54
55qmllint allows you to mark both properties and components as deprecated:
56
57\code
58@Deprecated { reason: "Use NewCustomText instead" }
59Text {
60 @Deprecated { reason: "Use newProperty instead" }
61 property int oldProperty
62 property int newProperty
63 Component.onCompleted: console.log(oldProperty); // Warning: XY.qml:8:40: Property "oldProperty" is deprecated (Reason: Use newProperty instead)
64}
65\endcode
66
67Deprecation warnings for components will be shown every time the component is created.
68
69\section2 Disabling warnings inline
70
71You may at any point disable warnings temporarily in a file using \c{// qmllint
72disable}.
73
74You can do this at the end of a line when a single line produces warnings:
75
76\code
77Item {
78 property string foo
79 Item {
80 property string bar: foo // qmllint disable unqualified
81 }
82}
83\endcode
84
85Alternatively you can disable comments for a block of lines by putting the
86comment in a line only containing \c{// qmllint disable}, ending the block with
87\c{// qmllint enable}:
88
89\code
90Item {
91 property string foo
92 Item {
93 // qmllint disable unqualified
94 property string bar: foo
95 property string bar2: foo
96 // qmllint enable unqualified
97 }
98}
99\endcode
100
101qmllint interprets all single line comments starting with \c {qmllint} as
102directives. Thus you may not start a comment that way unless you wish to enable
103or disable warnings.
104
105\note As done in the examples above it is preferable to explicitly specify the
106warning or a list of warnings you want to disable instead of disabling all
107warnings. This can be done by simply listing warning categories after \c{qmllint disable} (the names are
108the same as the options listed in \c{--help}).
109
110\section2 Settings
111
112In addition to passing command-line options, you can also
113configure qmllint via a settings file.
114The command line \c{--write-defaults} will generate one for you.
115
116Setting files are named \c{.qmllint.ini} and look like this:
117
118\quotefile qmllint/config.ini
119
120Warning levels may be set to \c{info}, \c{warning} or \c{disable} just as with
121command line options.
122
123qmllint will automatically look for a settings file at the location of the qml
124file that is being linted.
125It also looks through all parent directories to find this file and
126automatically applies the settings therein. You can disable this behavior by
127using \c{--ignore-settings}.
128You may always override these defaults by specifying command line parameters
129that take precedence over the warning levels in settings.
130
131\section2 Scripting
132
133qmllint can write or output JSON via the \c{--json <file>} option which will return valid JSON
134with warning messages, file and line location of warnings, and their severity
135level. Use the special filename '-' to write to stdout instead of a file.
136This can be used to more easily integrate qmllint in your pre-commit hooks or
137CI testing.
138
139\sa {Type Description Files}
140\sa {Qt Quick Tools and Utilities}
141*/