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