Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Loop of onChanged
Forum Updated to NodeBB v4.3 + New Features

Loop of onChanged

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 189 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mick_1
    wrote on last edited by
    #1

    Hello,

    I have a little problem that I can't solve.

    I have a value that I want to update when I press on my control. So far, no problem but this same value can be updated via other controls. So, I get into a loop between my onChanged and my application crashes...
    I simplified my problem with the example below.

    Window {
        id: root
        width: 640
        height: 480
        visible: true
    
        property int myValue: 0
    
        onMyValueChanged: {
            if (myValue >= 5)
            {
                switch1.checked = true;
            }
            else
            {
                switch1.checked = false;
            }
        }
    
    
        Slider {
            id: slider
            x: 350
            y: 212
            stepSize: 1
            to: 10
            value: myValue
    
            onValueChanged: myValue = value;
        }
    
        Switch {
            id: switch1
            x: 175
            y: 209
            text: qsTr("Switch")
    
            onCheckedChanged: {
                if (checked == true)
                {
                    myValue += 5;
                }
                else
                {
                   myValue -= 5;
                }
            }
        }
    }
    

    I want to update my value from two different places while keeping the "Controls" that can also drive it updated.
    How can I do this?

    Thank you for your help !

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      Don't create logical loops.

      Window {
          id: root
          width: 640
          height: 480
          visible: true
      
          property int myValue: 0
      
         
          Slider {
              id: slider
              x: 350
              y: 212
              stepSize: 1
              to: 10
              value: myValue
      
              onValueChanged: myValue = value;
          }
      
          Switch {
              id: switch1
              x: 175
              y: 209
              text: qsTr("Switch")
      
              checked: myValue >= 5
      
              onClicked: {
                  if(checked){
                       myValue += 5
                  }else{
                       myValue -= 5
                  }  
              }     
          }
      }
      

      Honestly though, you should have an indicator and 2 separate buttons to increment and decrement. Not sure what you are trying to do here.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved