导入QML文档目录

Importing QML Document Directories

导入QML文档目录

A local directory of QML files can be imported without any additional setup or configuration. A remote directory of QML files can also be imported, but requires a directory listing qmldir file to exist. A local directory may optionally contain a directory listing qmldir file in order to define the type names which should be provided to clients which import the directory, and to specify JavaScript resources which should be made available to importers.

可以导入QML文件的本地目录,而无需任何其他设置或配置。也可以导入QML文件的远程目录,但需要存在列出qmldir文件的目录。本地目录可以选择性地包含一个列出qmldir文件的目录,以定义应提供给导入目录的客户端的类型名称,并指定应提供给导入者的JavaScript资源。

Local Directory Imports

本地目录导入

Any QML file on the local file system can import a local directory as using an import statement that refers to the directory's absolute or relative file system path, enabling the file to use the object types defined within that directory.

​本地文件系统上的任何QML文件都可以使用引用目录的绝对或相对文件系统路径的import语句导入本地目录,从而使文件能够使用该目录中定义的对象类型。

If the local directory contains a directory listing qmldir file, the types will be made available with the type names specified in the qmldir file; otherwise, they will be made available with type names derived from the filenames of the QML documents. Only filenames beginning with an uppercase letter and ending with ".qml" will be exposed as types if no qmldir file is specified in the directory.

如果本地目录包含一个列出qmldir文件的目录,则这些类型将可用qmldir文件中指定的类型名称;否则,它们将使用从QML文档的文件名派生的类型名。如果目录中未指定qmldir文件,则只有以大写字母开头并以“.qml”结尾的文件名才会作为类型公开。

An Example

一个例子

Consider the following QML project directory structure. Under the top level directory myapp, there are a set of common UI components in a sub-directory named mycomponents, and the main application code in a sub-directory named main, like this:

考虑以下QML项目目录结构。在顶层目录myapp下,在名为mycomponents的子目录中有一组常见的UI组件,在名为main的子目录中有一组主应用程序代码,如下所示:

myapp
    |- mycomponents
        |- CheckBox.qml
        |- DialogBox.qml
        |- Slider.qml
    |- main
        |- application.qml

The main/application.qml file can import the mycomponents directory using the relative path to that directory, allowing it to use the QML object types defined within that directory:

main/application.qml文件可以使用mycomponents目录的相对路径导入该目录,从而使用该目录中定义的qml对象类型:

import "../mycomponents"

DialogBox {
    CheckBox {
        // ...
    }
    Slider {
        // ...
    }
}

The directory may be imported into a qualified local namespace, in which case uses of any types provided in the directory must be qualified:

目录可以导入到合格的本地命名空间中,在这种情况下,目录中提供的任何类型的使用都必须合格:

import "../mycomponents" as MyComponents

MyComponents.DialogBox {
    // ...
}

The ability to import a local directory is convenient for cases such as in-application component sets and application prototyping, although any code that imports such modules must update their relevant import statements if the module directory moves to another location. This can be avoided if QML modules are used instead, as an installed module is imported with a unique identifier string rather than a file system path.

​对于应用程序组件集和应用程序原型设计等情况,导入本地目录的功能很方便,但如果模块目录移动到另一个位置,导入此类模块的任何代码都必须更新其相关的导入语句。如果改用QML模块,这是可以避免的,因为已安装的模块是使用唯一标识符字符串而不是文件系统路径导入的。

Remotely Located Directories

远程目录

A directory of QML files can also be imported from a remote location if the directory contains a directory listing qmldir file.

如果QML文件目录包含列出qmldir文件的目录,也可以从远程位置导入该目录。

For example, if the myapp directory in the previous example was hosted at "http://www.my-example-server.com", and the mycomponents directory contained a qmldir file defined as follows:

例如,如果上一个示例中的myapp目录位于“http://www.my-example-server.com“,mycomponents目录包含一个qmldir文件,定义如下:

CheckBox CheckBox.qml
DialogBox DialogBox.qml
Slider Slider.qml

Then, the directory could be imported using the URL to the remote mycomponents directory:

然后,可以使用URL将目录导入远程mycomponents目录:

import "http://www.my-example-server.com/myapp/mycomponents"

DialogBox {
    CheckBox {
        // ...
    }
    Slider {
        // ...
    }
}

Note that when a file imports a directory over a network, it can only access QML and JavaScript files specified in the qmldir file located in the directory.

请注意,当文件通过网络导入目录时,它只能访问位于该目录中的qmldir文件中指定的QML和JavaScript文件。

Warning: When importing directories from a remote server, developers should always be careful to only load directories from trusted sources to avoid loading malicious code.

警告:当从远程服务器导入目录时,开发人员应始终注意仅加载来自受信任源的目录,以避免加载恶意代码。

Directory Listing qmldir Files

列出qmldir文件的目录

A directory listing qmldir file distinctly different from a module definition qmldir file. A directory listing qmldir file allows a group of QML documents to be quickly and easily shared, but it does not define a type namespace into which the QML object types defined by the documents are registered, nor does it support versioning of those QML object types.

​列出qmldir文件的目录与模块定义qmldir文件明显不同。列出qmldir文件的目录允许快速、轻松地共享一组QML文档,但它不定义一个类型命名空间,将文档定义的QML对象类型注册到该命名空间中,也不支持这些QML对象类型的版本控制。

The syntax of a directory listing qmldir file is as follows:

列出qmldir文件的目录的语法如下:

CommandSyntaxDescription
Object Type Declaration<TypeName> <FileName>

An object type declaration allows a QML document to be exposed with the given <TypeName>.

对象类型声明允许使用给定的<TypeName>公开QML文档。

Example:

例子:

RoundedButton RoundedBtn.qml
Internal Object Type Declarationinternal <TypeName> <FileName>

An internal object type declaration allows a QML document to be registered as a type which becomes available only to the other QML documents contained in the directory import. The internal type will not be made available to clients who import the directory.

内部对象类型声明允许将QML文档注册为仅对目录导入中包含的其他QML文档可用的类型。内部类型将不可用于导入目录的客户端。

Example:

例子:

internal HighlightedButton HighlightedBtn.qml
JavaScript Resource Declaration<Identifier> <FileName>

A JavaScript resource declaration allows a JavaScript file to be exposed via the given identifier.

JavaScript资源声明允许通过给定的标识符公开JavaScript文件。

Example:

例子:

MathFunctions mathfuncs.js

A local file system directory may optionally include a qmldir file. This allows the engine to only expose certain QML types to clients who import the directory. Additionally, JavaScript resources in the directory are not exposed to clients unless they are declared in a qmldir file.

本地文件系统目录可以选择性地包括qmldir文件。这允许引擎仅向导入目录的客户端公开某些QML类型。此外,目录中的JavaScript资源不会向客户端公开,除非它们在qmldir文件中声明。

© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值