Modules and Imports
Import QML modules, directories, and JavaScript files, configure
qmldir and singletons, and register native Rust types.
After reading this page, you will be able to import built-in modules, local directories, and JavaScript files, use namespace aliases, define singletons with
pragma Singleton and qmldir, and understand how Ljós discovers modules and native Rust types.Every
.qml file declares the modules, directories, and scripts it uses with import statements at the top of the file:Badge is parts/Badge.qml, a component of the imported directory:Rules.label is a function of rules.js:Ljós supports four forms of
import:Module imports (
import Ljos.Ui, import Ljos.Controls, import Ljos.Design, or the modules existing QML uses, such as import QtQuick, import QtQuick.Controls and import QtQuick.Layouts): bring the types and singletons of a named module into scope. Adding as <Namespace> qualifies those types and their enumerations under <Namespace> (for example, T.Control or T.ScrollBar.AlwaysOff).Directory imports (
import "parts" or import "look"): bring all .qml component files (and any pragma Singleton files or qmldir declarations) in that relative directory into scope.TypeScript imports (
import "filter.ts" as Logic): load a .ts module, check it, and expose what it exports under the qualifier Logic. Its types are checked against every call the QML makes.JavaScript imports (
import "rules.js" as Rules): load a .js file and expose its top-level functions and variables under the qualifier Rules.Enumeration keys can be read from any imported type, whether or not the script is inside an instance of that type (
Text.AlignHCenter, Font.Bold, ScrollBar.AlwaysOn, ScrollBar.Policy.AlwaysOff), or through the module's import namespace (T.ScrollBar.AlwaysOff).When building a project graph, Ljós discovers modules from
qmldir files and from CMake qt_add_qml_module definitions.A
qmldir file in a module or directory lists the module's types and singletons:Note that
qmldir directives depends, import, optional import, prefer, plugin, and typeinfo are not implemented yet.When resolving a relative URL with
Qt.resolvedUrl(path) or loading an asset from a component, Ljós resolves the address against the file where the call or component is written, following RFC 3986 (removing . and .. segments and keeping a trailing slash on directory paths).A QML singleton is a
.qml file that begins with pragma Singleton and is registered with a singleton line in qmldir (or imported from a directory):A singleton is created lazily the first time a file reads it. By default, one instance is shared across the engine; if a QML singleton reads a per-window native singleton, Ljós creates one instance per window.
Applications can expose native Rust types to QML using
#[derive(QmlObject)], #[qml_impl], and ljos::module!. Properties declared on a Rust type are final, and QtObject is its implied base type (objectName).During ahead-of-time compilation,
TypeOracle resolves types from three sources:1.
Ljós's built-in type table.
2.
An optional table of the upstream modules' types, which
ljos check --port compares against (see Bringing existing QML).3.
Your application's native Rust types, read either directly from
MetaObjects in an EngineOracle or from a ljostypes.json file.Running your application with
LJOS_TYPES=write writes out ljostypes.json, which you can pass to ljos::build::compile_dir_with_types (or let ljos::build::compile_dir pick up) so that unknown properties or handlers on your native Rust types are caught as compile errors at build time. ljos run reads the same file to preview a project without its Rust.


