mohsen
mohsen

Reputation: 1806

How Open a Dialog correctly in qt quick 2?

I have a custom Dialog like below:

CustomDialog.qml

 Dialog{    
    Component.onCompleted: {
        console.log("******* Loading Some Data from WebServcie ******")
    }
 }

Now I use this custom dialog in main.qml file:

 ApplicationWindow {
     id: mainWindow

        Button{
            id:btn
            onClicked: {
                cd.open();
            }
        }

    CustomDialog{
        id:cd
    }
}

When I run application,Component.onCompleted will executes,but I want this event execute after clicking the Button and after dialog will opend.How can I do this?

Upvotes: 0

Views: 388

Answers (1)

Blabdouze
Blabdouze

Reputation: 1121

If I understand correctly, you want to execute some actions when your Dialog appears on the screen.

You can do that by checking if the visible property has changed :

CustomDialog.qml

Dialog {
  onVisibleChanged: { 
    if (visible) {
       console.log("I'm visible now !")
    } 
  }
}

Note that Component.onCompleted is triggered when the component has been instantiated, not when the component is displayed to the user.

Upvotes: 1

Related Questions