Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
structure.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3/*!
4\page qtqml-documents-structure.html
5\title Structure of a QML Document
6\brief Description of the structure of QML documents
7
8
9A QML document is a self contained piece of QML source code that consists of two parts:
10
11 \list
12 \li Its \e import statements
13 \li A single root object declaration
14 \endlist
15
16By convention, a single empty line separates the imports from the object hierarchy definition.
17
18QML documents are always encoded in UTF-8 format.
19
20
21
22\section1 Imports
23
24A document must import the necessary modules or type namespaces to enable the
25engine to load the QML object types referenced within the document. By default,
26a document can access any QML object types that have been defined through
27\c .qml files in the same directory; if a document needs to refer to any other
28object types, it must import the type namespace into which those types have
29been registered.
30
31QML does \e not have a preprocessor that modifies the document prior to
32presentation to the \l{QQmlEngine}{QML engine}, unlike C or C++.
33The \c import statements do not copy and prepend the code in the document, but
34instead instruct the QML engine on how to resolve type references found
35in the document. Any type reference present in a QML document - such as \c
36Rectangle and \c ListView - including those made within a \l {JavaScript
37Expressions in QML Documents}{JavaScript block} or \l {Property Binding}{property
38bindings}, are \e resolved based exclusively on the import statements. At least
39one \c import statement must be present such as \c{import QtQuick 2.0}.
40
41Please see the \l{qtqml-syntax-imports.html}{QML Syntax - Import Statements}
42documentation for in-depth information about QML imports.
43
44
45\section1 The Root Object Declaration
46
47A QML document describes a hierarchy of objects which can be instantiated.
48Each object definition has a certain structure; it has a type, it can have an
49id and an object name, it can have properties, it can have methods, it can have
50signals and it can have signal handlers.
51
52A QML file must only contain \b {a single root object definition}. The following is invalid and will generate an error:
53
54\code
55// MyQmlFile.qml
56import QtQuick 2.0
57
58Rectangle { width: 200; height: 200; color: "red" }
59Rectangle { width: 200; height: 200; color: "blue" } // invalid!
60\endcode
61
62This is because a .qml file automatically defines a QML type, which encapsulates a \e single QML object definition. This is discussed further in \l{qtqml-documents-definetypes.html}{Documents as QML object type definitions}.
63
64*/