集成 QML 和 JavaScript

The QML language uses a JSON-like syntax and allows various expressions and methods to be defined as JavaScript functions. It also allows users to import JavaScript files and use the functionality those imports provide.

This allows developers and designers to leverage the knowledge they have of JavaScript to quickly develop both user-interfaces and application logic.

JavaScript 錶達式

QML has a deep JavaScript integration, and allows 信號處理程序 and methods to be defined in JavaScript. Another core feature of QML is the ability to specify and enforce relationships between object properties using 特性綁定 , which are also defined using JavaScript.

See the documentation page titled 在 QML 文檔中的 JavaScript 錶達式 for more information about using JavaScript expressions in QML.

從 JavaScript 動態創建 QML 對象

QML supports the dynamic creation of objects from within JavaScript. This is useful to delay instantiation of objects until necessary, thereby improving application startup time. It also allows visual objects to be dynamically created and added to the scene in reaction to user input or other events. This functionality can be used in two main ways.

Object can be created dynamically from JavaScript in an imperative way using 對象的動態創建 . This can be useful, for example, when QML is used as an application scripting language.

注意: When creating user interfaces, the preferred way of creating objects dynamically is to use declarative constructs as these integrate best with the QML engine and tooling. Various types exist to enable this functionality such as the Loader , Instantiator , Repeater 類型。

JavaScript 資源

Application logic defined in JavaScript functions may be separated into separate JavaScript files known as JavaScript resources. There are several different kinds of JavaScript resources, with different semantics.

See the documentation page titled 在 QML 中定義 JavaScript 資源 for more information about defining JavaScript resources for QML.

JavaScript 導入

A QML document may import JavaScript resources, and JavaScript resources may import other JavaScript resources as well as QML modules. This allows an application developer to provide application logic in modular, self-contained files.

See the documentation page titled 導入 JavaScript 資源 for more information on how to import JavaScript resources and how to use the functionality they provide.

JavaScript 主機環境

The QML engine provides a JavaScript environment that has some differences to the JavaScript environment provided by a web browser. Certain limitations apply to code running in the environment, and the QML engine provides various objects in the root context which may be unfamiliar to JavaScript developers.

These limitations and extensions are documented in the description of the JavaScript 主機環境 provided by the QML engine.

There is also an in depth description of the memory management employed by the JavaScript engine.

配置 JavaScript 引擎

For specific use cases you may want to override some of the parameters the JavaScript engine uses for handling memory and compiling JavaScript. See 配置 JavaScript 引擎 for more information on these parameters.