C
Import statements
Syntax of an import statement
An import statement allows you to list the modules you want to use within a QML document. The types that you want to use within a document depend on the modules and resources that you import.
Module (namespace) imports
Module import is the most common import type. You can import QML modules that register QML object types into a given namespace.
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 meaningful only for source compatibility with Qt Quick code. In Qt Quick Ultralite the<Version.Number>is ignored. Specifying a version does not change the set of available types or features. - The
<Qualifier>is an optional local namespace identifier, which you use to refer to the object types provided by the module. If omitted, the object types of the module are installed into the global namespace.
An example of an unqualified module import is as follows:
import QtQuickThis import lets you use all the types provided by the latest version of the QtQuick module without needing to specify a qualifier. For example, the code to create a rectangle is as follows:
import QtQuick
Rectangle {
width: 200
height: 100
color: "red"
}An example of a qualified module import is as follows:
import QtQuick as QuickThis import lets you import multiple modules which provide conflicting type names. However, each usage of a type provided by a module in a qualified namespace must use the qualifier prefix to resolve conflicts unambiguously.
An example of code which creates a rectangle after using a qualified module import is as follows:
import QtQuick as Quick
Quick.Rectangle {
width: 200
height: 100
color: "red"
}For more information about qualified imports, see Importing into a qualified local namespace.
Note: If a QML document refers or uses an object type that is not provided by any of the imported modules, an error will occur. For example, the following QML document does not import QtQuick and thus it cannot use the Rectangle type:
Rectangle {
width: 200
height: 100
color: "red"
}In this case, a compilation error will be shown.
Importing into a qualified local namespace
The import statement may optionally use the as keyword to import the types into a particular document-local namespace. If a namespace is specified, any references to the types made available by the import must be prefixed by the local namespace qualifier.
In the following example, the QtQuick module is imported into the "CoreItems" namespace. Any references to types from the QtQuick module must be prefixed with CoreItems:
import QtQuick 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. It does not become an attribute of the root object, which can be referred to externally as you can 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 part of different modules. In this case, you can import the two modules into different namespaces to ensure the code refers to the correct type:
import QtQuick as CoreItems
import MyCompany.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!" }
}In the example above, the MyCompany.TextWidgets URI must match the URI given when creating the module.
You can also import multiple modules into the same namespace. For example:
Available under certain Qt licenses.
Find out more.