Make title capitalization more consistent in QML documentation.
[profile/ivi/qtdeclarative.git] / src / qml / doc / src / documents / definetypes.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** GNU Free Documentation License
10 ** Alternatively, this file may be used under the terms of the GNU Free
11 ** Documentation License version 1.3 as published by the Free Software
12 ** Foundation and appearing in the file included in the packaging of
13 ** this file.
14 **
15 ** Other Usage
16 ** Alternatively, this file may be used in accordance with the terms
17 ** and conditions contained in a signed written agreement between you
18 ** and Nokia.
19 **
20 **
21 **
22 **
23 **
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27 /*!
28 \page qtqml-documents-definetypes.html
29 \title Defining Object Types through QML Documents
30 \brief Description of how a QML document is a reusable type definition
31
32 One of the core features of QML is that it enables QML object types to be easily defined in a lightweight manner through QML documents to suit the needs of individual QML applications. The standard QtQuick module provides various types like \l Rectangle, \l Text and \l Image for building a QML application; beyond these, you can easily define your own QML types to be reused within your application. This ability to create your own types forms the building blocks of any QML application.
33
34
35 \section1 Defining an Object Type with a QML File
36
37 To create an object type, a QML document should be placed into a text file named as \e <TypeName>.qml where \e <TypeName> is the desired name of the type, beginning with an uppercase letter. This document is then automatically recognized by the engine as a definition of a QML type. Additionally, a type defined in this manner is automatically made available to other QML files within the same directory as the engine searches within the immediate directory when resolving QML type names.
38
39 For example, below is a document that declares a \l Rectangle with a child \l MouseArea. The document has been saved to file named \c SquareButton.qml:
40
41 \qml
42 // SquareButton.qml
43 import QtQuick 2.0
44
45 Rectangle {
46     width: 100; height: 100
47     color: "red"
48
49     MouseArea {
50         anchors.fill: parent
51         onClicked: console.log("Button clicked!")
52     }
53 }
54 \endqml
55
56 Since the file is named \c SquareButton.qml, \b {this can now be used as a type named \c SquareButton by any other QML file within the same directory}. For example, if there was a \c myapplication.qml file in the same directory, it could refer to the \c SquareButton type:
57
58 \qml
59 // myapplication.qml
60 import QtQuick 2.0
61
62 SquareButton {}
63 \endqml
64
65 \image documents-definetypes-simple.png
66
67 This creates a 100 x 100 red \l Rectangle with an inner \l MouseArea, as defined in \c SquareButton.qml. When this \c myapplication.qml document is loaded by the engine, it loads the SquareButton.qml document as a component and instantiates it to create a \c SquareButton object.
68
69 The \c SquareButton type encapsulates the tree of QML objects declared in \c SquareButton.qml. When the QML engine instantiates a \c SquareButton object from this type, it is instantiating an object from the \l Rectangle tree declared in \c SquareButton.qml.
70
71 \note the letter case of the file name is significant on some (notably UNIX) filesystems. It is recommended the file name case matches the case of the desired QML type name exactly - for example, \c Box.qml and not \c BoX.qml - regardless of the platform to which the QML type will be deployed.
72
73 \section2 Importing Types Defined Outside the Current Directory
74
75 If \c SquareButton.qml was not in the same directory as \c myapplication.qml,
76 the \c SquareButton type would need to be specifically made available through an \e import statement in \c myapplication.qml. It could be imported from a relative path on the file system, or as an installed module; see \l {QML Modules}{module} for more details.
77
78
79 \section1 Accessible Attributes of Custom Types
80
81 The \b {root object} definition in a .qml file \b {defines the attributes that are available for a QML type}. All properties, signals and methods that belong to this root object - whether they are custom declared, or come from the QML type of the root object - are externally accessible and can be read and modified for objects of this type.
82
83 For example, the root object type in the \c SquareButton.qml file above is \l Rectangle. This means any properties defined by the \l Rectangle type can be modified for a \c SquareButton object. The code below defines three \c SquareButton objects with customized values for some of the properties of the root \l Rectangle object of the \c SquareButton type:
84
85 \qml
86 // application.qml
87 import QtQuick 2.0
88
89 Column {
90     SquareButton { width: 50; height: 50 }
91     SquareButton { x: 50; color: "blue" }
92     SquareButton { radius: 10 }
93 }
94 \endqml
95
96 \image documents-definetypes-attributes.png
97
98 The attributes that are accessible to objects of the custom QML type include any \l{Custom properties}{custom properties}, \l{Custom methods}{methods} and \l{Custom signals}{signals} that have additionally been defined for an object. For example, suppose the \l Rectangle in \c SquareButton.qml had been defined as follows, with additional properties, methods and signals:
99
100 \qml
101 // SquareButton.qml
102 import QtQuick 2.0
103
104 Rectangle {
105     id: root
106
107     property bool pressed: mouseArea.pressed
108
109     signal buttonClicked(real xPos, real yPos)
110
111     function showMousePosition() {
112         root.color = Qt.rgba(Qt.random(), Qt.random(), Qt.random(), 1)
113     }
114
115     width: 100; height: 100
116     color: "red"
117
118     MouseArea {
119         id: mouseArea
120         anchors.fill: parent
121         onClicked: root.buttonClicked(mouse.x, mouse.y)
122     }
123 }
124 \endqml
125
126 Any \c SquareButton object could make use of the \c pressed property, \c buttonClicked signal and \c randomizeColor() method that have been added to the root \l Rectangle:
127
128 \qml
129 // application.qml
130 import QtQuick 2.0
131
132 SquareButton {
133     id: squareButton
134
135     onButtonClicked: {
136         console.log("Clicked", xPos, yPos)
137         randomizeColor()
138     }
139
140     Text { text: squareButton.pressed ? "Down" : "Up" }
141 }
142 \endqml
143
144 Note that any of the \c id values defined in \c SquareButton.qml are not accessible to \c SquareButton objects, as id values are only accessible from within the component scope in which a component is declared. The \c SquareButton object definition above cannot refer to \c mouseArea in order to refer to the \l MouseArea child, and if it had an \c id of \c root rather than \c squareButton, this would not conflict with the \c id of the same value for the root object defined in \c SquareButton.qml as the two would be declared within separate scopes.
145
146
147 */