Checking Code Syntax

As you write code, Qt Creator checks code syntax. When Qt Creator spots a syntax error in your code it underlines it and shows error details when you move the mouse pointer over the error. Similarly, when you are working on an instance of a JavaScript object notation (JSON) entity, Qt Creator underlines errors in JSON data structure.

  • Syntax errors are underlined in red.

    In the following figure, a semicolon is missing at the end of the line.

  • Semantic errors and warnings are underlined in olive.

    In the following figure, the variable is not used.

In addition, you can run static checks on the QML and JavaScript code in your project to find common problems.

Checking JSON Data Structure

Qt Creator validates instances of JSON entities against A JSON Media Type for Describing the Structure and Meaning of JSON Documents. However, Qt Creator does not understand the entire specification.

A JSON schema defines the structure of JSON data. It determines what JSON data is required for an application and how to interact with it.

The specification does not define how to map JSON instances with JSON schemas. Qt Creator looks for a JSON schema file with a name that matches the name of the JSON instance file in the user configuration folder. For example, ~/config/QtProject/qtcreator/json on Linux and macOS and C:\Users\username\AppData\Roaming\QtCreator\qtcreator\json in Windows. To check JSON data structure, copy the JSON schema file to the above folder.

Checking JavaScript and QML Syntax

To run the checks, select Tools > QML/JS > Run Checks or press Ctrl+Shift+C. The results are shown in the QML Analysis filter of the Issues output pane.

List of JavaScript and QML Checks

Many of the JavaScript checks are similar to the ones in Douglas Crockford's JSLint tool.

IdSeverityMessageDescription
M1ErrorInvalid value for enum
M2ErrorEnum value must be a string or a number
M3ErrorNumber value expected
M4ErrorBoolean value expected
M5ErrorString value expected
M6ErrorInvalid URL
M7WarningFile or directory does not exist
M8ErrorInvalid color
M9ErrorAnchor line expected
M10ErrorDuplicate property binding
M11ErrorId expected
M14ErrorInvalid id
M15ErrorDuplicate idIds in a file must be unique.
M16ErrorInvalid property name name
M17ErrorName does not have members
M18ErrorField is not a member of object
M19WarningAssignment in conditionIt could be a typing error. If it is intentional, wrap the assignment in parentheses.
M20WarningUnterminated non-empty case blockCase blocks should either be empty or end in a flow control statement such as break, return or continue. Alternatively you can indicate intentional fall through by ending with a // fall through comment.
M23WarningDo not use eval
M28WarningUnreachableIndicates that the underlined statement will never be executed.
M29WarningDo not use with
M30WarningDo not use comma expressions
M31WarningUnnecessary message suppression
M103WarningName is already a formal parameter
M104WarningName is already a function
M105WarningVar name is used before its declaration
M106WarningName is already a var
M107WarningName is declared more than onceVariables declared in a function are always visible everywhere in the function, even when declared in nested blocks or for statement conditions. Redeclaring a variable has no effect.
M108WarningFunction name is used before its declaration
M109WarningDo not use Boolean as a constructor
M110WarningDo not use String as a constructor
M111WarningDo not use Object as a constructor
M112WarningDo not use Array as a constructor
M113WarningDo not use Function as a constructor
M114HintThe function keyword and the opening parenthesis should be separated by a single space
M115WarningDo not use stand-alone blocksBlocks do not affect variable scoping. Thus blocks that are not associated to if, while, etc. have no effect and should be avoided.
M116WarningDo not use void expressions
M117WarningConfusing pluses
M119WarningConfusing minuses
M121HintDeclare all function vars on a single line
M123HintUnnecessary parentheses
M126Warning== and != may perform type coercion, use === or !== to avoid itThe non-strict equality comparison is allowed to convert its arguments to a common type. That can lead to unexpected results such as ' \t\r\n' == 0 being true. Use the strict equality operators === and !== and be explicit about conversions you require.
M127WarningExpression statements should be assignments, calls or delete expressions only
M128ErrorA state cannot have the specified child item
M201HintPlace var declarations at the start of a function
M202HintUse only one statement per line
M203WarningImperative code is not supported in the Qt Quick Designer
M204WarningThis QML type is not supported in the Qt Quick Designer
M205WarningReference to parent QML type cannot be resolved correctly by the Qt Quick Designer
M206WarningThis visual property binding cannot be evaluated in the local context and might not show up in Qt Quick Designer as expected
M207WarningQt Quick Designer only supports states in the root QML type
M208ErrorThis type (type name) is not supported as a root element by Qt Quick Designer.
M220ErrorThis type (type name) is not supported as a root element of a Qt Quick UI form.
M221ErrorThis type (type name) is not supported in a Qt Quick UI form.
M222ErrorFunctions are not supported in a Qt Quick UI form.
M223ErrorJava Script blocks are not supported in a Qt Quick UI form.
M224ErrorBehavior type is not supported in a Qt Quick UI form.
M225ErrorStates are only supported in the root item in a Qt Quick UI form.
M226ErrorReferencing the parent of the root item is not supported in a Qt Quick UI form.
M300ErrorUnknown component
M301ErrorCould not resolve the prototype name of object
M302ErrorCould not resolve the prototype name
M303ErrorPrototype cycle, the last non-repeated component is name
M304ErrorInvalid property type name
M305Warning== and != perform type coercion, use === or !== to avoid itSee M126.
M306WarningCalls of functions that start with an uppercase letter should use newBy convention, functions that start with an uppercase letter are constructor functions that should only be used with new.
M307WarningUse new only with functions that start with an uppercase letter
M308WarningDo not use Number as a constructor
M309HintUse spaces around binary operators
M310WarningUnintentional empty block, use ({}) for empty object literal
M311HintUse type instead of var or variant to improve performance
M312ErrorMissing property number
M313ErrorObject value expected
M314ErrorArray value expected
M315ErrorValue value expected
M316ErrorMaximum number value is number
M317ErrorMinimum number value is number
M318ErrorMaximum number value is exclusive
M319ErrorMinimum number value is exclusive
M320ErrorString value does not match required pattern
M321ErrorMinimum string value length is number
M322ErrorMaximum string value length is number
M323ErrorNumber elements expected in array value
M324WarningUsing Qt Quick 1 code model instead of Qt Quick 2The code model might be corrupt or the QML emulation layer might have been built with a different Qt version than the one selected in the build and run kit. For more information, see Resetting the Code Model and Running QML Modules in Qt Quick Designer.

Resetting the Code Model

If you change the build and run kit when you have QML files open in the code editor, the code model might become corrupt. The following error message indicates that this might have happened: Using Qt Quick 1 code model instead of Qt Quick 2.

You can see the error message when you move the mouse pointer over code that Qt Creator underlines in the code editor or when you open a QML file in the Design mode.

To reset the code model, select Tools > QML/JS > Reset Code Model.

If this does not help, try changing the QML emulation layer to the one that was built with the same Qt version as the one selected in the build and run kit. For more information, see Running QML Modules in Qt Quick Designer.

Inspecting QML and JavaScript

To inspect QML and JavaScript properties, methods, and enums, move the cursor over them and select Tools > QML/JS > Inspect API for Element Under Cursor.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.