QML variable value get zeroed while changing the page

I am using stackView and with a button press I chance the page by using push and pop. I want to save same variable value and when I open the page again, I want to keep continue using that variable with its value when page is closed.That is why I declared the variables in the main qml :

Window{
     id:main
     property var isSomethingOn:false
     StackView{
        id:contentFrame
        initialItem:Qt.resolvedUrl("qrc:/MainPage.qml")
        Connections{
           target:contentFrame.currentItem
           onBackButtonPressed:{
              contentFrame.pop()
              }}}}}

and in the other page I use that variable like:

Item{
   id:Page1
   signal backButtonPressed()
   Image{
      id:button1
      MultiPointTouchArea{
         main.isSomethingOn = !main.isSomethingOn
         if(main.isSomethingOn){
            button2.buttonImageColor="Imgs/button_left_red.png"
         }
         else{
            button2.buttonImageColor="Imgs/button_left_blue.png"
         }
      }
}
  

Now I expect isSomethignOn to be saved and not to be deleted between page transitions. But it indeed get deleted. How can I solve that issue?

Upvotes: 0

Views: 170

Answers (1)

JarMan
JarMan

Reputation: 8277

The value is getting saved, but you probably can't tell because bindings don't automatically work with var properties. var properties do not automatically notify when their value changes. You can manually trigger them by calling isSomethingOnChanged(), but there's no reason to do that unless you actually need your property to be a var. In your case changing it to a bool should be all you need to do.

For your additional question about push and pop, see the docs. Particularly this line:

This means that any item pushed onto a StackView will never be destroyed by the StackView; only items that StackView creates from Components or URLs are destroyed by the StackView.

Upvotes: 1

Related Questions