ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Qt Quick Study Note -- No.2

2022-01-25 17:05:24  阅读:184  来源: 互联网

标签:CoreItems Qt -- Study namespace module QML import into


import与include类似
如果不确定使用某种类型时应当导入哪个模块, 可以使用Qt assisstant, 查看其所在的Qt Quick模块, 如下:
Text QML Type
Specifies how to add formatted text to a scene. More...

 

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 (Uniform Resource Identifier, 统一资源标志符) 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.

Example: import QtQuick 2.0

 

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.

Example: 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.

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


  import QtQuick 2.0 as Quick

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

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:


  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:


  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!" }
  }

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.

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.

 

JavaScript Resource Imports

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

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

 

  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.

......

 

标签:CoreItems,Qt,--,Study,namespace,module,QML,import,into
来源: https://www.cnblogs.com/henryliublog/p/15843631.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有