aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/doc/src/qmllanguageref/syntax/basics.qdoc
diff options
context:
space:
mode:
Diffstat (limited to 'src/qml/doc/src/qmllanguageref/syntax/basics.qdoc')
-rw-r--r--src/qml/doc/src/qmllanguageref/syntax/basics.qdoc190
1 files changed, 190 insertions, 0 deletions
diff --git a/src/qml/doc/src/qmllanguageref/syntax/basics.qdoc b/src/qml/doc/src/qmllanguageref/syntax/basics.qdoc
new file mode 100644
index 0000000000..cdfab3cd3f
--- /dev/null
+++ b/src/qml/doc/src/qmllanguageref/syntax/basics.qdoc
@@ -0,0 +1,190 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:FDL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Free Documentation License Usage
+** Alternatively, this file may be used under the terms of the GNU Free
+** Documentation License version 1.3 as published by the Free Software
+** Foundation and appearing in the file included in the packaging of
+** this file. Please review the following information to ensure
+** the GNU Free Documentation License version 1.3 requirements
+** will be met: http://www.gnu.org/copyleft/fdl.html.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+/*!
+\page qtqml-syntax-basics.html
+\title QML Syntax Basics
+\brief Description of the basics of QML syntax
+
+QML is a declarative language that enables objects to be defined in terms of their attributes
+and how they relate and respond to changes in other objects. In contrast to imperative code, where changes in attributes and behavior are expressed through a series of statements that are processed step by step, the declarative QML syntax integrates attribute and behavioral changes directly into the definitions of individual objects.
+
+QML source code is generally loaded by the engine through QML \e documents, which are
+standalone documents of QML code. These can be used to define \l {QML Object Types}{QML object types} that can then be reused throughout an application.
+
+
+\section1 Import Statements
+
+A QML document may have one or more imports at the top of the file.
+An import can be any one of:
+
+\list
+\li a versioned namespace into which types have been registered (e.g., by a plugin)
+\li a relative directory which contains type-definitions as QML documents
+\li a JavaScript file
+\endlist
+
+JavaScript file imports must be qualified when imported, so that the properties and methods they provide can be accessed.
+
+The generic form of the various imports are as follows:
+\list
+\li \tt{import Namespace VersionMajor.VersionMinor}
+\li \tt{import Namespace VersionMajor.VersionMinor as SingletonTypeIdentifier}
+\li \tt{import "directory"}
+\li \tt{import "file.js" as ScriptIdentifier}
+\endlist
+
+Examples:
+\list
+\li \tt{import QtQuick 2.0}
+\li \tt{import QtQuick.LocalStorage 2.0 as Database}
+\li \tt{import "../privateComponents"}
+\li \tt{import "somefile.js" as Script}
+\endlist
+
+Please see the \l{qtqml-syntax-imports.html}{QML Syntax - Import Statements}
+documentation for in-depth information about QML imports.
+
+
+\keyword qml-object-declarations
+\section1 Object Declarations
+
+Syntactically, a block of QML code defines a tree of QML objects to be created. Objects are
+defined using \e {object declarations} that describe the type of object to be created as well
+as the attributes that are to be given to the object. Each object may also declare child objects
+using nested object declarations.
+
+An object declaration consists of the name of its object type, followed by a set of curly braces. All attributes and child objects are then declared within these braces.
+
+Here is a simple object declaration:
+
+\qml
+Rectangle {
+ width: 100
+ height: 100
+ color: "red"
+}
+\endqml
+
+This declares an object of type \l Rectangle, followed by a set of curly braces that encompasses the attributes defined for that object. The \l Rectangle type is a type made available by the \c QtQuick module, and the attributes defined in this case are the values of the rectangle's \c width, \c height and \c color properties. (These are properties made available by the \l Rectangle type, as described in the \l Rectangle documentation.)
+
+The above object can be loaded by the engine if it is part of a \l{qtqml-documents-topic.html}{QML document}. That is, if the source code is complemented with \e import statement that imports the \c QtQuick module (to make the \l Rectangle type available), as below:
+
+\qml
+import QtQuick 2.0
+
+Rectangle {
+ width: 100
+ height: 100
+ color: "red"
+}
+\endqml
+
+When placed into a \c .qml file and loaded by the QML engine, the above code creates a \l Rectangle object using the \l Rectangle type supplied by the \c QtQuick module:
+
+\image qtqml-syntax-basics-object-declaration.png
+
+\note If an object definition only has a small number of properties, it can be written on a single line like this, with the properties separated by semi-colons:
+
+\qml
+Rectangle { width: 100; height: 100; color: "red" }
+\endqml
+
+Obviously, the \l Rectangle object declared in this example is very simple indeed, as it defines nothing more than a few property values. To create more useful objects, an object declaration may define many other types of attributes: these are discussed in the \l{qtqml-syntax-objectattributes.html}{QML Object Attributes} documentation. Additionally, an object declaration may define child objects, as discussed below.
+
+
+\section2 Child Objects
+
+Any object declaration can define child objects through nested object declarations. In this way, \b {any object declaration implicitly declares an object tree that may contain any number of child objects}.
+
+For example, the \l Rectangle object declaration below includes a \l Gradient object declaration,
+which in turn contains two \l GradientStop declarations:
+
+\qml
+import QtQuick 2.0
+
+Rectangle {
+ width: 100
+ height: 100
+
+ gradient: Gradient {
+ GradientStop { position: 0.0; color: "yellow" }
+ GradientStop { position: 1.0; color: "green" }
+ }
+}
+\endqml
+
+When this code is loaded by the engine, it creates an object tree with a \l Rectangle object at the root; this object has a \l Gradient child object, which in turn has two \l GradientStop children.
+
+Note, however, that this is a parent-child relationship in the context of the QML object tree, not
+in the context of the visual scene. The concept of a parent-child relationship in a visual scene is provided by the \l Item type from the \c QtQuick module, which is the base type for most QML types, as most QML objects are intended to be visually rendered. For example, \l Rectangle and \l Text are both \l {Item}-based types, and below, a \l Text object has been declared as a visual child of a \l Rectangle object:
+
+\qml
+import QtQuick 2.0
+
+Rectangle {
+ width: 200
+ height: 200
+ color: "red"
+
+ Text {
+ anchors.centerIn: parent
+ text: "Hello, QML!"
+ }
+}
+\endqml
+
+When the \l Text object refers to its \l {Item::parent}{parent} value in the above code, it is referring to its \e {visual parent}, not the parent in the object tree. In this case, they are one and the same: the \l Rectangle object is the parent of the \l Text object in both the context of the QML object tree as well as the context of the visual scene. However, while the \l {Item::parent}{parent} property can be modified to change the visual parent, the parent of an object in the context of the object tree cannot be changed from QML.
+
+(Additionally, notice that the \l Text object has been declared without assigning it to a property of the \l Rectangle, unlike the earlier example which assigned a \l Gradient object to the rectangle's \c gradient property. This is because the \l {Item::children}{children} property of \l Item has been set as the type's \l {qtqml-syntax-objectattributes.html#default-properties}{default property} to enable this more convenient syntax.)
+
+See the \l{qtquick-visualcanvas-visualparent.html}{visual parent} documentation for more information on the concept of visual parenting with the \l Item type.
+
+
+\section1 Comments
+
+The syntax for commenting in QML is similar to that of JavaScript:
+
+\list
+\li Single line comments start with // and finish at the end of the line.
+\li Multiline comments start with /* and finish with *\/
+\endlist
+
+\snippet qml/comments.qml 0
+
+Comments are ignored by the engine when processing QML code. They are useful for explaining what a section of code is doing, whether for reference at a later date or for explaining the implementation to others.
+
+Comments can also be used to prevent the execution of code, which is sometimes useful for tracking down problems.
+
+\qml
+ Text {
+ text: "Hello world!"
+ //opacity: 0.5
+ }
+\endqml
+
+In the above example, the \l Text object will have normal opacity, since the line opacity: 0.5 has been turned into a comment.
+*/