In questa pagina

Integrazione di JavaScript in QML

Il codice JavaScript può essere facilmente integrato in QML per fornire logica dell'interfaccia utente, controllo imperativo o altri vantaggi.

Utilizzo di espressioni JavaScript per i valori delle proprietà

Le espressioni JavaScript possono essere utilizzate in QML come binding. Ad esempio:

Item {
    width: Math.random()
    height: width < 100 ? 100 : (width + 50) /  2
}

Si noti che le chiamate di funzione, come Math.random(), non verranno rivalutate a meno che i loro argomenti non cambino. Pertanto, il binding a Math.random() restituirà un numero casuale e non verrà rivalutato, ma se la larghezza viene modificata in qualche altro modo, il binding dell'altezza verrà rivalutato per tenerne conto.

Aggiunta di funzioni JavaScript in QML

Le funzioni JavaScript possono essere dichiarate sugli elementi QML, come nell’esempio seguente. Ciò consente di chiamare il metodo utilizzando l’ID dell’elemento.

import QtQuick

Item {
    id: container
    width: 320
    height: 480

    function randomNumber() {
        return Math.random() * 360;
    }

    function getNumber() {
        return container.randomNumber();
    }

    TapHandler {
        // This line uses the JS function from the item
        onTapped: rectangle.rotation = container.getNumber();
    }

    Rectangle {
        color: "#272822"
        width: 320
        height: 480
    }

    Rectangle {
        id: rectangle
        anchors.centerIn: parent
        width: 160
        height: 160
        color: "green"
        Behavior on rotation { RotationAnimation { direction: RotationAnimation.Clockwise } }
    }

}

Utilizzo dei file JavaScript

I file JavaScript possono essere utilizzati per estrarre la logica dai file QML. Per farlo, inserisci innanzitutto le tue funzioni all’interno di un file .js come nell’esempio mostrato.

// myscript.js
function getRandom(previousValue) {
    return Math.floor(previousValue + Math.random() * 90) % 360;
}

Quindi importare il file in qualsiasi file .qml che necessiti di utilizzare le funzioni, come nel file QML di esempio riportato di seguito.

import QtQuick
import "myscript.js" as Logic

Item {
    width: 320
    height: 480

    Rectangle {
        color: "#272822"
        width: 320
        height: 480
    }

    TapHandler {
        // This line uses the JS function from the separate JS file
        onTapped: rectangle.rotation = Logic.getRandom(rectangle.rotation);
    }

    Rectangle {
        id: rectangle
        anchors.centerIn: parent
        width: 160
        height: 160
        color: "green"
        Behavior on rotation { RotationAnimation { direction: RotationAnimation.Clockwise } }
    }

}

Quadrato verde ruotato di un angolo calcolato da JavaScript

Per ulteriori dettagli sul motore JavaScript utilizzato da QML, nonché sulla differenza rispetto al JavaScript dei browser, consultare la documentazione completa sulle espressioni JavaScript nei documenti QML.

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.