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. Problem in handling parallel states in Qt/Qml
Forum Updated to NodeBB v4.3 + New Features

Problem in handling parallel states in Qt/Qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 547 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.
  • P Offline
    P Offline
    PradeepChinnasamy
    wrote on last edited by
    #1

    I am trying to set my view in two states at the same time with two different conditions with when property, but only the first condition works and the respective states are set. Though the value in the second condition changes, the states are not set. My example here...

    import QtQuick 2.0
    import QtQuickUltralite.Extras 2.0
    
    Item {
        id: signal
    
        property int show: 0
        property int  showtwo: 0
    
        Rectangle{ id: rect1; width: 50; height: 50; color: "green"; visible: false}
        Rectangle{ id: rect2; y:100; width: 50; height: 50; color: "red"; visible: false}
    
        Rectangle{ id: rect3; x:100; width: 50; height: 50; color: "yellow"; visible: false}
        Rectangle{ id: rect4; x:100; y:100; width: 50; height: 50; color: "black"; visible: false}
    
        states: [
            State {
                name: "green"
                when: signal.show === 0
                PropertyChanges{ target: rect1; visible: true}
            },
            State {
                name: "red"
                when: signal.show === 1
                PropertyChanges{ target: rect2; visible: true}
            },
            State {
                name: "yellow"
                when: signal.showtwo === 0
                PropertyChanges{ target: rect3; visible: true}
            },
            State {
                name: "black"
                when: signal.showtwo === 1
                PropertyChanges{ target: rect4; visible: true}
            }
        ]
    
        Timer {
            running: true; repeat: true; interval: 1000
    
            onTriggered: {
                console.log(signal.show)
                if(signal.show === 0)
                {
                    signal.show = 1
                    signal.showtwo = 1
                }
                else
                {
                    signal.show = 0
                    signal.showtwo = 0
                }
            }
        }
    }
    

    Though the values are changed for signal.showtwo, the corresponding states "yellow" and "black" are never set. What is missing here ? How can I achieve this in qml ?

    I am using QtforMCU v2.0.0. But the behavior is same in Qt5.x as well.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You cannot have 2 active states in a single item at the same time. Either move the states to corresponding rectangles (then they can be completely independent), or combine them together, like so:

      • red and black
      • green and yellow
      • etc.

      (Z(:^

      P 1 Reply Last reply
      1
      • sierdzioS sierdzio

        You cannot have 2 active states in a single item at the same time. Either move the states to corresponding rectangles (then they can be completely independent), or combine them together, like so:

        • red and black
        • green and yellow
        • etc.
        P Offline
        P Offline
        PradeepChinnasamy
        wrote on last edited by
        #3

        @sierdzio Thanks for your answer, it gives clarity on the states.

        1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          Or don't use states at all.

          import QtQuick 2.0
          import QtQuickUltralite.Extras 2.0
          
          Item {
              id: signal
          
              property bool show: false
              property bool  showtwo: false
          
              Rectangle{ id: rect1; width: 50; height: 50; color: "green"; visible: !signal.show }
              Rectangle{ id: rect2; y:100; width: 50; height: 50; color: "red"; visible: signal.show }
          
              Rectangle{ id: rect3; x:100; width: 50; height: 50; color: "yellow"; visible: !signal.showtwo }
              Rectangle{ id: rect4; x:100; y:100; width: 50; height: 50; color: "black"; visible: signal.showtwo }
          
              Timer {
                  running: true; repeat: true; interval: 1000
          
                  onTriggered: {
                      console.log(signal.show);
                      signal.show = !signal.show;
                      signal.showtwo = !signal.showtwo;
                  }
              }
          }
          
          1 Reply Last reply
          1

          • Login

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