在QT中创建Window中使用方法

Using a method in the created Window in QT

现在有一个激活功能的按钮,例如:

Window {
    id: window
    width: Screen.width
    height: Screen.height
    visible: true
    property alias originalImage: originalImage
    title: qsTr("Window1")
    
    Button{
        id: startButton
        x: 50
        y:100
        text: "Open"

        onClicked: {
            VideoStreamer.openVideoCamera()
        }
}

但我想激活此方法 just after the window is created,而不是单击按钮。喜欢

Window {
    id: window
    width: Screen.width
    height: Screen.height
    visible: true
    property alias originalImage: originalImage
    title: qsTr("Window1")

    VideoStreamer.openVideoCamera()
}

但事实并非如此。我收到 expected token ":" 错误。它需要类似 somecondition: 我如何在没有按钮或没有外部用户需要的条件(如 onclicked: 等)的情况下管理它?

您可以使用 Component.onCompleted 信号在创建组件后立即执行操作。

可以在任何对象上声明 onCompleted 信号处理程序。 运行 处理程序的顺序未定义。

Window {
    id: window
    width: Screen.width
    height: Screen.height
    visible: true
    property alias originalImage: originalImage
    title: qsTr("Window1")
    
    Component.onCompleted: 
    {
        VideoStreamer.openVideoCamera()
    }
}

P.S.: 如果你的 VideoStreamer 是一个 QML 对象,那么直接使用这个信号可能会更好。