Import Statements 导入语句

Syntax of an Import Statement

导入语句的语法

An import statement allows clients to tell the engine which modules, JavaScript resources and component directories are used within a QML document. The types which may be used within a document depends on which modules, resources and directories are imported by the document.

导入语句允许客户告诉引擎QML文档内可使用哪些模块、JavaScript资源和组件目录。文档内可以使用哪些类型依赖于被文档导入的模块、资源和目录。

Import Types

导入类型

There are three different types of imports. Each import type has a slightly different syntax, and different semantics apply to different import types.

有3种不同类型的导入。每种导入类型在语法上有细微的差别,不同的语义应用到不同的导入类型。

Module (Namespace) Imports

模块的导入

The most common type of import is a module import. Clients can import QML modules which register QML object types and JavaScript resources into a given namespace.

最常见的导入是模块导入。客户可以导入注册了对象类型的模块和JavaScript资源到给定的命名空间。

The generic form of a module import is as follows:

模块导入的一般形式如下:

import <ModuleIdentifier> <Version.Number> [as <Qualifier>]

The <ModuleIdentifier> is an identifier specified in dotted URI notation, which uniquely identifies the type namespace provided by the module.

The <Version.Number> is a version of the form MajorVersion.MinorVersion which specifies which definitions of various object types and JavaScript resources will be made available due to the import.

The <Qualifier> is an optional local namespace identifier into which the object types and JavaScript resources provided by the module will be installed, if given. If omitted, the object types and JavaScript resources provided by the module will be installed into the global namespace.

<ModuleIdentifier>是点分URI表示法指定的标识符,此标识符是模块提供的命名空间类型的唯一识别。

<Version.Number>是大版本.小版本格式的版本号。指定各对象类型和JavaScript资源的哪个版本哪个定义将被提供给导入。

<Qualifier>是可选的本地命名空间,如果给出,它作为模块提供的对象类型和JavaScript资源的识别(命名空间)被安装。如果省略,模块提供的对象类型和JavaScript资源将被安装到全局命名空间。

An example of an unqualified module import is as follows:

如下是一个无限定的模块导入的例子:

import QtQuick 2.0

This import allows the use of all of the types provided by the QtQuick module without needing to specify a qualifier. For example, the client code to create a rectangle is as follows:

这样导入允许使用 QtQuick 模块提供的所有类型,不需要指定限定(的命名空间)。例如,客户代码可以创建 rectangle 如下:

import QtQuick 2.0

Rectangle {
    width: 200
    height: 100
    color: "red"
}

An example of a qualified module import is as follows:

如下是一个限定的模块导入:

import QtQuick 2.0 as Quick

This import allows multiple modules which provide conflicting type names to be imported at the same time, however since each usage of a type provided by a module which was imported into a qualified namespace must be preceded by the qualifier, the conflict is able to be resolved unambiguously by the QML engine.

这样允许同时导入多个提供的类型有名称冲突的模块,由于每一个类型都由被导入限定命名空间的模块提供,所以在使用时必须在前边加上限定符(可以理解为命名空间),冲突可以通过QML引擎明确地解决掉。

An example of client code which creates a rectangle after using a qualified module import is as follows:

如下是一个使用限定式模块导入的客户代码创建一个 rectangle 的例子:

import QtQuick 2.0 as Quick

Quick.Rectangle {
    width: 200
    height: 100
    color: "red"
}

For more information about qualified imports, see the upcoming section on Importing Into A Qualified Local Namespace.

关于限定式导入的更多信息,请看接下来的章节<Importing Into A Qualified Local Namespace>导入到限定的本地命名空间。

Note that if a QML document does not import a module which provides a particular QML object type, but attempts to use that object type anyway, an error will occur.

注意,如果QML文档不导入提供具体QML对象类型的模块,但是尝试使用那个对象类型,将产生错误。

For example, the following QML document does not import QtQuick and thus attempting to use the Rectangle type will fail:

例如,如下的QML文档不导入 QtQuick 并尝试使用 Rectangle 类型,将失败:

Rectangle {
    width: 200
    height: 100
    color: "red"
}

In this case, the engine will emit an error and refuse to load the file.

这种情况,引擎将发射一个错误并拒绝载入文件。

Non-module Namespace Imports

无模块命名空间导入

Types can also be registered into namespaces directly via the various registration functions in C++ (such as qmlRegisterType()). The types which have been registered into a namespace in this way may be imported by importing the namespace, as if the namespace was a module identifier.

类型也可以在C++中通过各种注册函数直接注册到命名空间(例如 qmlRegisterType() 函数)。被注册进命名空间的类型可以通过导入命名空间被导入,如同命名空间是一个模块标识。

This is most common in client applications which define their own QML object types in C++ and register them with the QML type system manually.

这是客户端程序中最常用,在C++中定义自己的QML对象类型并且在QML类型系统手动地注册他们。

Importing into a Qualified Local Namespace

导入限定的本地命名空间

The import statement may optionally use the as keyword to specify that the types should be imported into a particular document-local namespace. If a namespace is specified, then any references to the types made available by the import must be prefixed by the local namespace qualifier.

导入语句可以选择性的使用关键字去指定类型应该被导入特定的的本地文档命名空间。如果指定命名空间,那么想引用任何被导入的类型都必须以本地命名空间前缀限定。

Below, the QtQuick module is imported into the namespace "CoreItems". Now, any references to types from the QtQuick module must be prefixed with the CoreItems name:

如下,QtQuick 模块被导入进命名空间 "CoreItems" 。现在,任何引用 QtQuick 模块的类型都必须使用 CoreItems 前缀:

import QtQuick 2.0 as CoreItems

CoreItems.Rectangle {
    width: 100; height: 100

    CoreItems.Text { text: "Hello, world!" }

    // WRONG! No namespace prefix - the Text type won‘t be found
    Text { text: "Hello, world!" }
}

A namespace acts as an identifier for a module within the scope of the file. The namespace does not become an attribute of the root object that can be referred to externally as can be done with properties, signals and methods.

命名空间作为文件范围内的模块的标识。命名空间不像属性、信号和方法一样被操作,不会变成可引用到外部的根对象的属性.

The namespaced import is useful if there is a requirement to use two QML types that have the same name but are located in different modules. In this case the two modules can be imported into different namespaces to ensure the code is referring to the correct type:

命名空间入口是有用的,如果需要使用2个QML类型有相同的名字但是位于不同的模块.这种情况下2个模块可以被导入不同的命名空间以确保代码引用正确的类型:

import QtQuick 2.0 as CoreItems
import "../textwidgets" as MyModule

CoreItems.Rectangle {
    width: 100; height: 100

    MyModule.Text { text: "Hello from my custom text item!" }
    CoreItems.Text { text: "Hello from Qt Quick!" }
}

Note that multiple modules can be imported into the same namespace in the same way that multiple modules can be imported into the global namespace. For example:

注意多模块可以被导入同一个命名空间,同样的多模块可以被被导入全局命名空间.例如:

import QtQuick 2.0 as Project
import QtMultimedia 5.0 as Project

Project.Rectangle {
    width: 100; height: 50

    Project.Audio {
        source: "music.wav"
        autoPlay: true
    }
}

Directory Imports

目录的导入

A directory which contains QML documents may also be imported directly in a QML document. This provides a simple way for QML types to be segmented into reusable groupings: directories on the filesystem.

包含QML文档的目录也可以被直接导入到QML文档。这提供了一个简单的方法以把QML类型分割成可复用的组:文件系统上的目录。

The generic form of a directory import is as follows:

目录导入的格式通常如下:

import "<DirectoryPath>" [as <Qualifier>]

Note: Import paths are network transparent: applications can import documents from remote paths just as simply as documents from local paths. See the general URL resolution rules for Network Transparency in QML documents. If the directory is remote, it must contain a directory import listing qmldir file as the QML engine cannot determine the contents of a remote directory if that qmldir file does not exist.

注意:导入路径是网络透明的:应用程序可以从远程路径导入文档如同从本地路径导入一样简单。查看通用的QML文档的网络透明的URL解析规则。如果目录是远程的,它必须包含一个(目录导入清单)qmldir文件,如果qmldir文件不存在QML引擎不能确定远程目录的内容。

Similar semantics for the <Qualifier> apply to directory imports as for module imports; for more information on the topic, please see the previous section about Importing into a Qualified Local Namespace.

<Qualifier>应用到目录导入和模块导入有相同的语义;更多信息请查看前一章节<Importing into a Qualified Local Namespace>。

For more information about directory imports, please see the in-depth documentation about directory imports.

关于目录导入的更多信息,请查看更深入的文档<directory imports>。

JavaScript Resource Imports

JavaScript资源的导入

JavaScript resources may be imported directly in a QML document. Every JavaScript resource must have an identifier by which it is accessed.

JavaScript资源可以被直接导入到QML文档。每一个JavaScript资源必须有一个针对它的访问者的标识。

The generic form of a JavaScript resource import is as follows:

JavaScript资源的导入格式通常如下:

import "<JavaScriptFile>" as <Identifier>

Note that the <Identifier> must be unique within a QML document, unlike the local namespace qualifier which can be applied to module imports.

注意<Identifier>必须是QML文档内独一无二的,不同于可应用到模块入口的本地命名空间限定符。

JavaScript Resources from Modules

来自模块的JavaScript资源

Javascript files can be provided by modules, by adding identifier definitions to the qmldir file which specifies the module.

模块可以提供Javascript文件,通过添加标识说明到qmldir文件,qmldir文件是用来指定模块的。

For example, if the projects.MyQMLProject.MyFunctions module is specified with the following qmldir file, and installed into the QML import path:

例如,如果projects.MyQMLProject.MyFunctions模块被如下的qmldir文件指定,并且安装在QML的入口路径:

module projects.MyQMLProject.MyFunctions
SystemFunctions 1.0 SystemFunctions.js
UserFunctions 1.0 UserFunctions.js

a client application is able to import the JavaScript resources declared in the module by importing the module and using the identifier associated with a declared resource:

客户端应用程序可以通过导入模块来导入模块内的已声明的JavaScript资源,并且使用标识关联到对应的已声明的资源。

import QtQuick 2.0
import projects.MyQMLProject.MyFunctions 1.0

Item {
    Component.onCompleted: { SystemFunctions.cleanUp(); }
}

If the module was imported into a document-local namespace, the JavaScript resource identifiers must be prefixed with the namespace qualifier in order to be used:

如果模块被导入到文档局部的命名空间,JavaScript资源标识必须以命名空间限定符作前缀,才能被使用:

import QtQuick 2.0
import projects.MyQMLProject.MyFunctions 1.0 as MyFuncs
import org.example.Functions 1.0 as TheirFuncs

Item {
    Component.onCompleted: {
        MyFuncs.SystemFunctions.cleanUp();
        TheirFuncs.SystemFunctions.shutdown();
    }
}

Further Information

补充信息

For more information about JavaScript resources, please see the documentation about defining JavaScript resources in QML, and for more information about how to import JavaScript resources, and how imports can be used from within JavaScript resources, please see the in-depth documentation about importing JavaScript resources in QML.

关于JavaScript资源的更多信息,请查看QML中文档<defining JavaScript resources in QML>在QML中定义JavaScript资源,更多的关于导入JavaScript资源的信息,如何在JavaScript资源内部导入,请查看更深入的文档<importing JavaScript resources in QML>在QML中导入JavaScript资源。

QML Import Path

QML 导入路径

When an identified module is imported, the QML engine searches the import path for a matching module.

当识别的模块被导入,QML 引擎查找匹配的模块的导入路径。

This import path, as returned by QQmlEngine::importPathList(), defines the default locations to be searched by the engine. By default, this list contains:

这个导入路径,作为 QQmlEngine::importPathList() 的返回值,定义引擎搜索的默认为止。默认情况下,列表包含:

1 The directory of the current file

2 The location specified by QLibraryInfo::Qml2ImportsPath

3 Paths specified by the QML2_IMPORT_PATH environment variable

Additional import paths can be added through QQmlEngine::addImportPath() or the QML2_IMPORT_PATH environment variable. When running the qmlscene tool, you can also use the -I option to add an import path.

附加导入路径可以通过 QQmlEngine::addImportPath() 或者环境变量 QML2_IMPORT_PATH 添加。当运行 qmlscene 工具,可以使用 -I 选项添加导入路径。

Debugging

The QML_IMPORT_TRACE environment variable can be useful for debugging when there are problems with finding and loading modules. See Debugging module imports for more information.

当查找和载入模块有问题时,环境变量 QML_IMPORT_TRACE 对调试有用。更详细的信息请查看调试模块的导入。

时间: 2024-08-04 14:03:50

Import Statements 导入语句的相关文章

Java记录 -20- 包与导入语句

Java 包与导入语句 包(package):用于将完成不同功能的类分门别类,放在不同的目录(包)下. 包的命名规则:一般讲公司域名反转作为包名.对于包名,每个字母都需要小写. 如果定义类的时候没有使用package,那么java就认为我们所定义的类位于默认包里面(default package). 编译带有package声明的Java源文件有两种方式: 1).直接编译,然后根据类中所定义的包名,逐一手工建立了目录结构,最后将生成的class文件放到改目录结构中(很少使用,比较麻烦). 2).使

包与导入语句剖析

包(package).用于将完成不同功能的类分门别类,放在不同的目录(包)下.包的命名规则:将公司域名反转作为包名.如:com.baidu,对于包名:每个字母都需要小写.如果定义类时候,没有使用package,那么Java就认为我们所定义的类位于默认包里面(default package). 编译带有package声明的Java源文件有两种方式: a)直接编译,然后根据类中所定义的包名,逐一手工建立目录结构,最后将生成的class文件放到该目录结构中(很少使用,比较麻烦). b)使用编译参数-d

ThinkPHP import 类库导入 include PHP文件

ThinkPHP 模拟了 Java 的类库导入机制,统一采用 import 方法进行类文件的加载.import 方法是 ThinkPHP 内建的类库和文件导入方法,提供了方便和灵活的文件导入机制,完全可以替代 PHP 的 require 和 include 方法. 但对于第三方类库,我们建议使用 vendor 方法来导入. import 语法: boolen import(class, baseUrl, ext) 参数说明: 参数 说明 class 必须,表示要导入的类库,采用命名空间的方式.

二、Java面向对象(7)_封装思想——static import(静态导入)

2018-04-30 static import(静态导入) import static 是静态导入,是jdk1.5的新特征,可以不同过调用包名,直接使用包里的静态方法. 语法: 1)import static 包名.类或接口名.静态成员名; 2)import static 包名.类或接口名.*; 此时的*表示当前类的任意使用到的静态成员 举个例子来说: 用import的情况: public static void main(String args[]){ System.out.println(

使用neo4j图数据库的import工具导入数据 -方法和注意事项

背景 最近我在尝试存储知识图谱的过程中,接触到了Neo4j图数据库,这里我摘取了一段Neo4j的简介: Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中.它是一个嵌入式的.基于磁盘的.具备完全的事务特性的Java持久化引擎,但是它将结构化数据存储在网络(从数学角度叫做图)上而不是表中.Neo4j也可以被看作是一个高性能的图引擎,该引擎具有成熟数据库的所有特性.程序员工作在一个面向对象的.灵活的网络结构下而不是严格.静态的表中--但是他们可以享受到具备完全的事务

@Import直接导入类,在容器@Component中是怎么直接导入类注册到容器的?

Java程序员必学技术:@Import直接导入类,在容器@Configuration.@Component中是怎么直接导入类注册到容器的? Spring IoC 容器是一个管理 Bean 的容器,在 Spring 的 定义中,它要求所有的 IoC 容器都需要实现接口 BeanFactory ,它是一个顶级容器接口 . 接下来就和大家分享一下, //直接插入一个类,既可以注册到容器 //这里注意注册的Bean的名称是全类名 @Import(Stu.class) @Configuration pub

Python基础----import模块导入和包的调用

模块概念 在Python中,一个.py文件就称之为一个模块(Module).使用模块组织代码,最大的好处是大大提高了代码的可维护性 模块一共三种:python标准库.第三方模块.应用程序自定义模块. 相同名字的函数和变量完全可以分别存在不同的模块中,因此,我们自己在编写模块时,不必考虑名字会与其他模块冲突.但是也要注意,尽量不要与内置函数名字冲突. 模块导入 import语句 1 import module1[, module2[,... moduleN] 使用import语句时候,Python

使用 from import方法导入Python模块

比如我们导入一个数学计算的模块 math: >>> import math>>> print math<module 'math' (built-in)>>>>>>> print math.pi #导出圆周率的值3.14159265359>>> 我们导入math模块,在python模块学习中我们会知道,这样做会得到名math的对象,这个模块对象包含了pi这样的常量,以及一些其它的方法. 我们如果直接访问

Python Day05 python 环境变量和import模块导入

1.定义 模块:本质就是.py结尾的文件(逻辑上组织python代码)模块的本质就是实现一个功能 文件名就是模块名称 包: 一个有__init__.py的文件夹:用来存放模块文件 2.导入模块 import 模块名 form 模块名 import * from 模块名 import 模块名 as 新名称 3. 导入模块本质 import 模块名 ===> 将模块中所有的数据赋值给模块名,调用时需要模块名.方法名() from 模块名 import 方法名 ==>将该方法单独放到当前文件运行一遍