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, displays an icon, and shows error details when you move the mouse pointer over the error or the icon. If a fix is available, you can apply it by clicking .

Similarly, when you are working on an instance of a JavaScript object notation (JSON) entity, Qt Creator underlines errors in the JSON data structure.

In the following screenshot, Qt Creator annotates an error because a semicolon is missing at the end of the line:

In the following screenshot, Qt Creator issues a warning because the variable is not used:

In the following screenshot, a Qt class name contains a typo:

You can use the icons in the tooltip popup to copy the error or warning message to the clipboard or to ignore and hide it. Select the Annotation Settings link to specify the position of the line annotations.

To modify the colors used for underlining errors and warnings, select Edit > Preferences > Text Editor > Font & Colors > Copy, and select new colors for Error and Warning.

When writing QML or JavaScript code, the annotations may contain error codes listed in JavaScript and QML Error Codes.

Specifying Line Annotation Positions

To specify the position where the annotations are displayed, select Edit > Preferences > Text Editor > Display > Line annotations, and then select whether to display the annotations directly next to the code, aligned to the right of the code, or in the right margin. Showing annotations between lines can be useful if there is usually not enough space to display annotations next to the text.

"Text Editor Display preferences"

If you hide the annotations by deselecting the check box, you can move the mouse pointer over an icon to view them.

JavaScript and QML Error Codes

You can run static checks on the QML and JavaScript code in your project to find common problems. To run the checks, select Tools > QML/JS > Run Checks or press Ctrl+Shift+C. The results are shown in the QML and QML Analysis filters in Issues.

Many of the error messages are similar to the ones in Douglas Crockford's JSLint tool. For more information about JSLint errors, see JSLint Error Explanations.

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 bindingSee also: Duplicate key '{a}'.
M11ErrorId expectedSee also: Expected an identifier and instead saw '{a}' (a reserved word).
M14ErrorInvalid idSee also: Expected an identifier and instead saw '{a}' (a reserved word).
M15ErrorDuplicate idIds in a file must be unique. See also: Duplicate key '{a}'.
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 evalSee also: eval is evil.
M28WarningUnreachableIndicates that the underlined statement will never be executed.
M29WarningDo not use withSee also: Unexpected '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 already is 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 declarationSee also: {a} was used before it was defined.
M109WarningDo not use Boolean as a constructorSee also: Do not use {a} as a constructor.
M110WarningDo not use String as a constructorSee also: Do not use {a} as a constructor.
M111WarningDo not use Object as a constructorSee also: Do not use {a} as a constructor.
M112WarningDo not use Array as a constructorSee also: Do not use {a} as a constructor.
M113WarningDo not use Function as a constructorSee also: Do not use {a} as a constructor.
M114HintThe function keyword and the opening parenthesis should be separated by a single spaceSee also: Expected exactly one space between {a} and {b}.
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 plusesSee also: Confusing pluses.
M119WarningConfusing minusesSee also: Confusing 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
M129ErrorType cannot be instantiated recursively
M201HintPlace var declarations at the start of a functionSee also: Move 'var' declarations to the top of the function.
M202HintUse only one statement per line
M220ErrorThis type (type name) is not supported as a root element of a UI file (.ui.qml)For more information about supported QML types, see UI Files.
M221ErrorThis type (type name) is not supported in a UI file (.ui.qml)For more information about supported QML types, see UI Files.
M222ErrorFunctions are not supported in a UI file (.ui.qml)For a list of supported JavaScript functions, see Supported Methods.
M223ErrorJavaScript blocks are not supported in a UI file (.ui.qml)For more information about supported features, see UI Files.
M224ErrorBehavior type is not supported in a UI file (.ui.qml)For more information about supported QML types, see UI Files.
M225ErrorStates are only supported in the root item in a UI file (.ui.qml)For more information about supported features, see UI Files.
M226ErrorReferencing the parent of the root item is not supported in a UI file (.ui.qml)For more information about supported features, see UI Files.
M227ErrorDo not mix translation functions in a UI file (.ui.qml)Even though several different translation functions, such as qsTr and qsTrId are supported, you should pick one and use it consistently within a UI file. For more information, see Qt QML Methods.
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 letterSee also: Do not use {a} as a constructor.
M308WarningDo not use Number as a constructorSee also: Do not use {a} 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 valueSee also: The array literal notation [] is preferable.
M324ErrorHit maximum recursion limit visiting AST, the code model will be unreliable and most likely invalid
M325WarningLogical value does not depend on actual values
M326ErrorComponents are only allowed to have a single child elementFor more information, see Component.
M327WarningComponents require a child elementFor more information, see Component.
M400WarningDuplicate importAn import statement has been added twice. For more information, see Import Statements.

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 Qt Design Studio.

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.

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.

Automatically Formatting QML/JS Files

To automatically format QML/JS files upon saving, select Edit > Preferences > Qt Quick > QML/JS Editing > Enable auto format on file save.

"QML/JS Editing preferences"