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