Explanation of QML 2

Source: Internet
Author: User
QML document

The QML document is a string consisting of QML syntax. A document defines a QML object type. The document is suffixed with ". qml" and can be saved locally and on the network. It can be generated using code. An instance of the object type defined in the document can also be created using Component in QML code or using QQmlComponent in C ++. Once an object type is explicitly registered using a specific name in the QML object system, this type can be found in other documents.

It is directly used to declare objects. Defining reusable QML object types in the document is very important for allowing the client to write modules. It is also highly readable and

More maintainable.

QML document structure

The QML document consists of two parts: the import part and the object declaration part. The import section in the document contains the import description and defines the QML objects available in the document.

Type and JavaScript resource. The object declaration part defines the object tree. When the object type is instantiated and defined in the document, the object tree will be created.

A simple document example is as follows:

Separate the import part and object declaration part with an empty line. QML documents are typically coded in UTF-8.

Import

A document must import necessary modules or object namespaces to ensure that the engine can load the QML object types declared in the document. By default, QML documentation

You can access any qml object type defined by the. QML file in the same directory. If a document needs to access other object types, it must import the object namespace.

Unlike C or C ++, before the QML engine loads a document, QML does not use a Preprocessor to pre-process the document. The Import declaration does not copy and present its code in the document, but instructs QML how to parse these object types. Any types that appear in the QML document-including Rectangle and ListView-constitute a JavaScript block or attribute binding, which are parsed based on the import declaration. At least one import declaration is required: import QtQuick2.0.

Root object Declaration

A qml document describes an object hierarchy that can be instantiated. Each object definition contains a certain structure: an object has a type, an id, and an object name. It can also contain attributes, including methods, signals, and signal processing handles.

A qml document must contain only one single root object definition. The following document is invalid and will produce an error:

This is because a. qml file automatically defines a QML type, which encapsulates the definition of a single QML object.

QML syntax

The appropriate QML syntax must be used to specify valid objects in the object declaration section. An object declaration may contain custom object attributes specified by geothermal. The object method attribute can be specified in a similar way to a JavaScript method. The object attribute can be specified using an attribute binding expression.

You can view the QML syntax for more information, or view integrated QML and JavaScript for more information.

Define object types through QML documents

We briefly described how to define a QML object type in a QML document. The core principle of QML is to define reusable object types. This improves the maintainability of the QML code, increases the readability of the code level declaration, and improves the separation of UI Interface Definition and logical implementation.

In the following example, the client developer defines the human Button type in the document:

The Button type can be used in the application as follows:

Resource loading and Network Transmission

QML can be transmitted over the network. Applications can import documents in the remote path as they do in the local path. In fact, any url attribute can be specified as a local or remote URL, and the QML engine automatically processes any content related to network communication.

QML uses URLs instead of file names to obtain referenced content in QML documents to support network transmission. This means that any URL resource can be used, and QML can process remote resources like local resources. For example:

Because the relative URL is the same as the relative file, you can use the following simple expression to replace the above:

Network Transmission is supported throughout the QML, for example:

* Fonts -- the source attribute of FontLoader is a URL;

* WebViews -- url attribute of WebView;

Even the QML type itself is also on the network-If qmlscence is used to load metrics
The engine loads the http://example.com/mystuff/world.qmlfile. In the Hello. qml file, other resources referenced by the URL are also loaded from the network in a similar way.

Relative and absolute URLs

Here, you only need to remember that when using an absolute URL, QML will not do any processing and will be used directly. If the relative URL is used, then QML will be converted to an absolute URL before use.

QRC Resources

The URL syntax expression is set to "qrc" in Qt. This allows applications to use the Qt resource system to compile the content into executable files. By using QRC resources, you can compile QML documents into executable programs.

In this case, you can use relative URLs, which do not need to be transmitted because the qml content is directly compiled into the executable program.

Restrictions

Network Transmission is triggered only when the "as" expression is used in the import declaration.

More details:

* Import "dir" only works in the local file system

* ImportlibraryUri only works in the local file system

* Import "dir" asD can be used during network transmission.

* Importlibraryuri as U can be used during network transmission.

Significance of application security

The QML security model is that the QML content is trustworthy: Users install QML content like they install native Qt applications, or programs written at runtime, such as Python and Perl. This trust is built on a series of mechanisms, including the packet signature mechanism used on some platforms.

To save users' trust, QML application developers cannot load and execute any JavaScript or QML resources. For example, the following QML code:

This is equivalent to downloading and executing http://evil.com/evil.exe. The QML engine does not block specific resource downloads. Unlike JavaScript code execution in a browser, QML applications can load local and remote resources in the same way as other applications. Therefore, application developers must be careful enough to load and execute tasks.

Content. QML must verify the data loaded by an application that exceeds its control. For example, do not use import, Loader, or XMLHttpRequest to load any unreliable code or content.

Namespace and name resolution

Most expressions in this document are called objects or object attributes. Because multiple objects may be defined and different attributes of different objects have the same name, the syntax for symbolic parsing must be predefined in QML. For this part, see namespace and symbol parsing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.