Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
topic.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-topic.html
5
\title QML Documents
6
\brief Description of QML documents
7
8
A QML document is a string which conforms to QML document syntax. A document
9
defines a QML object type. A document is generally loaded from a \c ".qml"
10
file stored either locally or remotely, but can be constructed manually in
11
code. An instance of the object type defined by a document may be created
12
using a \l Component in QML code, or a \l QQmlComponent in C++.
13
Alternatively, if the object type is explicitly exposed to the QML type system
14
with a particular type name, the type may be used directly in object
15
declarations in other documents.
16
17
The ability to define re-usable QML object types in documents is an important
18
enabler to allow clients to write modular, highly readable and maintainable
19
code.
20
21
Since Qt 5.4, a document can also have the file extension \c ".ui.qml". The QML
22
engine handles these files like standard .qml files and ignores the \c .ui part
23
of the extension. Qt Design Studio handles those files as
24
\l{Qt Design Studio: UI Files}{UI files}. The files can contain only a subset
25
of the QML language features.
26
27
\section1 Structure of a QML Document
28
29
A QML document consists of two sections: the imports section, and the object
30
declaration section. The imports section in a document contains import
31
statements that define which QML object types and JavaScript resources the
32
document is able to use. The object declaration section defines the object
33
tree to be created when instantiating the object type defined by the document.
34
35
An example of a simple document is as follows:
36
37
\qml
38
import QtQuick 2.0
39
40
Rectangle {
41
width: 300
42
height: 200
43
color: "blue"
44
}
45
\endqml
46
47
See the \l {qtqml-documents-structure.html}{Structure of a QML Document}
48
for more information on the topic.
49
50
\section2 Syntax of the QML Language
51
52
The object declaration section of the document must specify a valid object
53
hierarchy with appropriate \l {qtqml-syntax-basics.html}{QML syntax}. An
54
object declaration may include the specification of custom
55
\l{qtqml-syntax-objectattributes.html}{object attributes}. Object method
56
attributes may be specified as JavaScript functions, and object property
57
attributes may be assigned \l{qtqml-syntax-propertybinding.html}
58
{property binding expressions}.
59
60
Please see the documentation about the \l{qtqml-syntax-basics.html}
61
{syntax of QML} for more information about valid syntax, and see the
62
documentation about \l{qtqml-javascript-topic.html}
63
{integrating QML and JavaScript} for in-depth information on that topic.
64
65
\section1 Defining Object Types Through QML Documents
66
67
As described briefly in the previous section, a document implicitly defines
68
a QML object type. One of the core principles of QML is the ability to define
69
and then re-use object types. This improves the maintainability of QML code,
70
increases the readability of object hierarchy declarations, and promotes
71
separation between UI definition and logic implementation.
72
73
In the following example, the client developer defines a \c Button type with
74
a document in a file:
75
76
\snippet ../quick/doc/snippets/qml/qml-extending-types/components/Button.qml 0
77
78
The \c Button type can then be used in an application:
79
80
\table
81
\row
82
\li \snippet ../quick/doc/snippets/qml/qml-extending-types/components/application.qml 0
83
\li \image button-types.png {Application.qml creates a column that contains
84
three instances of the button type; each with their own size and color
85
values}
86
\endtable
87
88
Please see the documentation about \l{qtqml-documents-definetypes.html}
89
{defining object types in documents} for in-depth information on the
90
topic.
91
92
\section1 Resource Loading and Network Transparency
93
94
It is important to note that QML is network-transparent. Applications can
95
import documents from remote paths just as simply as documents from local
96
paths. In fact, any \c url property may be assigned a remote or local URL,
97
and the QML engine will handle any network communication involved.
98
99
Please see the \l{qtqml-documents-networktransparency.html}
100
{Network Transparency} documentation for more information about network
101
transparency in imports.
102
103
\section1 Scope and Naming Resolution
104
105
Expressions in documents usually involve objects or properties of objects,
106
and since multiple objects may be defined and since different objects may have
107
properties with the same name, some predefined symbol resolution semantics must
108
be defined by QML. Please see the page on \l{qtqml-documents-scope.html}
109
{scope and symbol resolution} for in-depth information about the topic.
110
111
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
documents
topic.qdoc
Generated on
for Qt by
1.14.0