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