Qt QML Type

  • Import Statement: import QtQml

  • Group: Qt is part of qml-utility-elements

Detailed Description

Qt is a singleton type that provides utility functions, properties, and enums. Here is an example showing how to use this type:

 
Sélectionnez
import QtQuick 2.0

Text {
    color: Qt.rgba(1, 0, 0, 1)
    text: Qt.md5("hello, world")
}

Enums

The Qt object contains the enums available in the Qt Namespace. For example, you can access the Qt::LeftButton and Qt::RightButton enumeration values as Qt.LeftButton and Qt.RightButton.

Types

The Qt object also contains helper functions for creating objects of specific data types. This is primarily useful when setting the properties of an item when the property has one of the following types:

If the QtQuick module has been imported, the following helper functions for creating objects of specific data types are also available for clients to use:

There are also string based constructors for these types. See QML Basic Types for more information.

Date/Time Formatters

Dynamic Object Creation

The following functions on the global object allow you to dynamically create QML items from files or strings. See Dynamic QML Object Creation from JavaScript for an overview of their use.

Other Functions

Property Documentation

 

[since 5.1] application : object

The application object provides access to global application state properties shared by many QML components.

It is the same as the Application singleton.

The following example uses the application object to indicate whether the application is currently active:

 
Sélectionnez
import QtQuick 2.0

Rectangle {
    width: 300; height: 55
    color: Qt.application.active ? "white" : "lightgray"
    Text {
        text: "Application " + (Qt.application.active ? "active" : "inactive")
        opacity: Qt.application.active ? 1.0 : 0.5
        anchors.centerIn: parent
    }
}

When using QML without a QGuiApplication, the following properties will be undefined:

  • application.active

  • application.state

  • application.layoutDirection

  • application.font

This property was introduced in Qt 5.1.

[since 5.0] inputMethod : object

The inputMethod object allows access to application's QInputMethod object and all its properties and slots. See the QInputMethod documentation for further details.

This property was introduced in Qt 5.0.

[since 5.1] platform : object

The platform object provides info about the underlying platform.

Its properties are:

platform.os

This read-only property contains the name of the operating system.

Possible values are:

  • "android" - Android

  • "ios" - iOS

  • "tvos" - tvOS

  • "linux" - Linux

  • "osx" - macOS

  • "qnx" - QNX (since Qt 5.9.3)

  • "unix" - Other Unix-based OS

  • "windows" - Windows

  • "wasm" - WebAssembly

platform.pluginName

This is the name of the platform set on the QGuiApplication instance as returned by QGuiApplication::platformName()

This property was introduced in Qt 5.1.

[since 5.5] styleHints : object

The styleHints object provides platform-specific style hints and settings. See the QStyleHints documentation for further details.

The styleHints object is only available when using the Qt Quick module.

The following example uses the styleHints object to determine whether an item should gain focus on mouse press or touch release:

 
Sélectionnez
import QtQuick 2.4

MouseArea {
    id: button

    onPressed: {
        if (!Qt.styleHints.setFocusOnTouchRelease)
            button.forceActiveFocus()
    }
    onReleased: {
        if (Qt.styleHints.setFocusOnTouchRelease)
            button.forceActiveFocus()
    }
}

This property was introduced in Qt 5.5.

[since 5.15] uiLanguage : string

The uiLanguage holds the name of the language to be used for user interface string translations. It is exposed in C++ as QQmlEngine::uiLanguage property.

You can set the value freely and use it in bindings. It is recommended to set it after installing translators in your application. By convention, an empty string means no translation from the language used in the source code is intended to occur.

If you're using QQmlApplicationEngine and the value changes, QQmlEngine::retranslate() will be called.

This property was introduced in Qt 5.15.

Method Documentation

 

[since 5.8] callLater(function)

[since 5.8] callLater(function, argument1, argument2, ...)

Use this function to eliminate redundant calls to a function or signal.

The function passed as the first argument to Qt.callLater() will be called later, once the QML engine returns to the event loop.

When this function is called multiple times in quick succession with the same function as its first argument, that function will be called only once.

For example:

 
Sélectionnez
import QtQuick 2.0

Rectangle {
    width: 480
    height: 320

    property int callsToUpdateMinimumWidth: 0
    property bool optimize: true

    property int currentTextModel: 0
    property var columnTexts: [
        ["Click on either", "rectangle above", "and note how the counter", "below updates", "significantly faster using the", "regular (non-optimized)", "implementation"],
        ["The width", "of this column", "is", "no wider than the", "widest item"],
        ["Note how using Qt.callLater()", "the minimum width is", "calculated a bare-minimum", "number", "of times"]
    ]

    Text {
        x: 20; y: 280
        text: "Times minimum width has been calculated: " + callsToUpdateMinimumWidth
    }

    Row {
        y: 25; spacing: 30; anchors.horizontalCenter: parent.horizontalCenter
        Rectangle {
            width: 200; height:  50; color: "lightgreen"
            Text { text: "Optimized behavior\nusing Qt.callLater()"; anchors.centerIn: parent }
            MouseArea { anchors.fill: parent; onClicked: { optimize = true; currentTextModel++ } }
        }
        Rectangle {
            width: 200; height:  50; color: "lightblue"
            Text { text: "Regular behavior"; anchors.centerIn: parent}
            MouseArea { anchors.fill: parent; onClicked: { optimize = false; currentTextModel++ } }
        }
    }

    Column {
        id: column
        anchors.centerIn: parent

        onChildrenChanged: optimize ? Qt.callLater(updateMinimumWidth) : updateMinimumWidth()

        property int widestChild
        function updateMinimumWidth() {
            callsToUpdateMinimumWidth++
            var w = 0;
            for (var i in children) {
                var child = children[i];
                if (child.implicitWidth > w) {
                    w = child.implicitWidth;
                }
            }

            widestChild = w;
        }

        Repeater {
            id: repeater
            model: columnTexts[currentTextModel%3]
            delegate: Text {
                color: "white"
                text: modelData
                width: column.widestChild
                horizontalAlignment: Text.Center
                Rectangle { anchors.fill: parent; z: -1; color: index%2 ? "gray" : "darkgray" }
            }
        <