Home > OS >  Using a method in the created Window in QT
Using a method in the created Window in QT

Time:11-20

Right now, there is a button for activating the function, like:

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()
        }
}

But I want to activate this method just after the window is created, not with a button click. Like

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

    VideoStreamer.openVideoCamera()
}

But it doesn't work like that. I get expected token ":" error. It expects something like somecondition: How am I going to manage this without a button, or without an external user-needed condition like onclicked:, etc. ?

CodePudding user response:

You can use Component.onCompleted signal to do things right after component is created.

The onCompleted signal handler can be declared on any object. The order of running the handlers is undefined.

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

P.S.: if your VideoStreamer is an QML object maybe it is a better to use this signal directly there.

  •  Tags:  
  • c qt
  • Related