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