How to know when a qml starts to being displayed and when it is hidden



  • I need to create a custom qml ( a rectangle with components inside) that needs to receive information from a service only when it is being displayed, so it must register to the service when starts to be displayed and unregister when it is hidden. I tried using Component.onCompleted and Component.onDestruction but the problem with events is they belongs to the Component lifecycle and not to the Item lifecycle. What events I need to listen to implement the desired behaviour?



  •  onVisibleChanged: {
            if (visible)
                // is being displayed
            else 
               // is being hidden
    }
    


  • What exactly do you mean by "displayed" and "hidden"? Hidden by the application? Hidden by a window being minimized or covered by another window?



  • @xargs1 The case I am asking for is when it becomes visible to the user or hidden to the user. You provided a good example, hidden because the user opened a new window and the current one is not displayed anymore, or because I have a StackView and I push a new item to the StackView, in this case the previous item dissapears but if I pop the StackView it appears again. I need to know those changes to start and stop listening to an streaming service. I was thinking to do it inside the item code using some events but if it is not possible I should do it from outside when the parent item make changes to the UI.



  • StackView provides its own attached property that will tell you when an item becomes visible or hidden. The Window element has a Visibility attached property that might tell you what you want to know, though I doubt it handles "covered by another window".



  • @xargs1 by being displayed I mean the window visiblility changed from false to true and being hidden the window visibilty changed from true to false :) I usually use states binded to the windows visibilty, that way I ensure that I only have one windows visible at the time.
    It is up to the developer to choose best option to fit its needs, and this is just one possible example. Of course this wont work in one shows a window above another using the z order, for example.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.