QML应用程序启动后立即执行JS代码

Hed*_*dge 2 qt qml

我的QML应用程序中有一个ApplicationWindow。我想在加载后立即执行一些Javascript代码,但是没有找到这样的处理程序(例如onLoaded)。

我该怎么做?

MrE*_*Sir 5

您要查找的处理程序是Component.onCompleted。这是一个简单的例子:

import QtQuick 2.2
import QtQuick.Controls 1.1

ApplicationWindow {
    visible: true
    width: 500
    height: 500

    Rectangle {
        id: rect
        anchors.fill: parent

        // First paint a red rectangle
        color: "red"
    }

    Component.onCompleted: {
        // Make it blue when we load
        rect.color = "blue"
    }
}
Run Code Online (Sandbox Code Playgroud)