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. Is it possible to set condition base on color for OnClicked ?
QtWS25 Last Chance

Is it possible to set condition base on color for OnClicked ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 521 Views
  • 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.
  • K Offline
    K Offline
    kkai295
    wrote on last edited by
    #1

    I have set 2 rectangles with 2 different colors. Only when both rectangles are turn red by the users, the user can click on a third button. I have been trying to get the third button to work base on detecting the colors of the previous 2 rectangles. Does it work???? or is there an alternative to detect the color ??

    Is it possible to set condition base on color for OnClicked ?
    MouseArea
    { anchors.fill: parent
    onClicked: { if (apple.color = "#E3B4AA")
    {pear.color ='white'}
    else {pear.color='green'}}
    }

    mfatihM 1 Reply Last reply
    0
    • K kkai295

      I have set 2 rectangles with 2 different colors. Only when both rectangles are turn red by the users, the user can click on a third button. I have been trying to get the third button to work base on detecting the colors of the previous 2 rectangles. Does it work???? or is there an alternative to detect the color ??

      Is it possible to set condition base on color for OnClicked ?
      MouseArea
      { anchors.fill: parent
      onClicked: { if (apple.color = "#E3B4AA")
      {pear.color ='white'}
      else {pear.color='green'}}
      }

      mfatihM Offline
      mfatihM Offline
      mfatih
      wrote on last edited by
      #2

      @kkai295 I believe so, but I advise you to use colorEqual method

      import QtQuick 2.12
      import QtQuick.Window 2.12
      
      Window {
          visible: true
          width: 270
          height: 270
          title: qsTr("Hello World")
      
          Rectangle {
              id: rect1
              x: 20; y: 20
              width: 100; height: 100
              color:"blue"
              MouseArea {
                 anchors.fill: parent
                 onClicked: Qt.colorEqual(parent.color, "red")?parent.color = "blue" : parent.color="red"
             }
          }
      
          Rectangle {
              id: rect2
              x: 150; y: 20
              width: 100; height: 100
              color:"blue"
              MouseArea {
                 anchors.fill: parent
                 onClicked: Qt.colorEqual(parent.color, "red")?parent.color = "blue" : parent.color="red"
             }
          }
      
          Rectangle {
              id: rect3
              x: 85; y: 150
              width: 100; height: 100
              color:"blue"
      
              MouseArea {
                 anchors.fill: parent
                 onClicked: {
                     // Only change state when two other rectangle color is red
                     if (Qt.colorEqual(rect1.color, "red") && Qt.colorEqual(rect2.color, "red"))
                     {
                         Qt.colorEqual(parent.color, "red")?parent.color = "blue" : parent.color="red"
                     }
                 }
             }
          }
      }
      
      

      By the way, you can also utilize states for this purpose as illustrated below:

      import QtQuick 2.12
      import QtQuick.Window 2.12
      
      Window {
          visible: true
          width: 270
          height: 270
          title: qsTr("Hello World")
      
          Rectangle {
              id: rect1
              x: 20; y: 20
              width: 100; height: 100
              state:"blueState"
              MouseArea {
                 anchors.fill: parent
                 onClicked: rect1.state == "redState"?rect1.state="blueState":rect1.state="redState"
             }
      
              states: [
                  State {
                     name: "redState";
                     PropertyChanges { target: rect1; color: "red";  }},
                  State {
                     name: "blueState";
                     PropertyChanges { target: rect1; color: "blue";  }
                  }
              ]
          }
      
          Rectangle {
              id: rect2
              x: 150; y: 20
              width: 100; height: 100
              state:"blueState"
              MouseArea {
                 anchors.fill: parent
                 onClicked: rect2.state == "redState"?rect2.state="blueState":rect2.state="redState"
             }
      
              states: [
                  State {
                     name: "redState";
                     PropertyChanges { target: rect2; color: "red";  }},
                  State {
                     name: "blueState";
                     PropertyChanges { target: rect2; color: "blue";  }
                  }
              ]
          }
      
          Rectangle {
              id: rect3
              x: 85; y: 150
              width: 100; height: 100
              state:"blueState"
      
              MouseArea {
                 anchors.fill: parent
                 onClicked: {
                     // Only change state when two other rectangle color is red
                     if (rect1.state == "redState" && rect2.state=="redState")
                     {
                         rect3.state == "redState"?rect3.state="blueState":rect3.state="redState"
                     }
                 }
             }
      
              states: [
                  State {
                     name: "redState";
                     PropertyChanges { target: rect3; color: "red";  }},
                  State {
                     name: "blueState";
                     PropertyChanges { target: rect3; color: "blue";  }
                  }
              ]
          }
      }
      
      

      You can find more about me and my post at www.yazilimperver.com

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

        Done without verbose state or complex imperative expressions, just an additional boolean property for each Rectangle and by binding the enabled property of the last Rectangle to the active property of the 2 others.
        My advice is not to base your logic and the UI properties of your objects.
        Stuff like redState or blueState aren't very semantic, and more like an implementation detail.

        import QtQuick 2.12
        import QtQuick.Window 2.12
        
        Window {
            visible: true
            width: 270
            height: 270
            title: qsTr("Hello World")
        
            Rectangle {
                id: rect1
                x: 20; y: 20
                width: 100; height: 100
                property bool active: false
                color: active ? "blue" : "red"
                MouseArea {
                   anchors.fill: parent
                   onClicked: parent.active = !parent.active
               }
            }
        
            Rectangle {
                id: rect2
                x: 150; y: 20
                width: 100; height: 100
                property bool active: false
                color: active ? "blue" : "red"
                MouseArea {
                   anchors.fill: parent
                   onClicked: parent.active = !parent.active
               }
            }
        
            Rectangle {
                id: rect3
                x: 85; y: 150
                width: 100; height: 100
        
                property bool active: false
                color: active ? "blue" : "red"
                enabled: rect1.active && rect2.active
                MouseArea {
                   anchors.fill: parent
                   onClicked: parent.active = !parent.active
               }
            }
        }
        
        1 Reply Last reply
        2
        • mfatihM mfatih

          @kkai295 I believe so, but I advise you to use colorEqual method

          import QtQuick 2.12
          import QtQuick.Window 2.12
          
          Window {
              visible: true
              width: 270
              height: 270
              title: qsTr("Hello World")
          
              Rectangle {
                  id: rect1
                  x: 20; y: 20
                  width: 100; height: 100
                  color:"blue"
                  MouseArea {
                     anchors.fill: parent
                     onClicked: Qt.colorEqual(parent.color, "red")?parent.color = "blue" : parent.color="red"
                 }
              }
          
              Rectangle {
                  id: rect2
                  x: 150; y: 20
                  width: 100; height: 100
                  color:"blue"
                  MouseArea {
                     anchors.fill: parent
                     onClicked: Qt.colorEqual(parent.color, "red")?parent.color = "blue" : parent.color="red"
                 }
              }
          
              Rectangle {
                  id: rect3
                  x: 85; y: 150
                  width: 100; height: 100
                  color:"blue"
          
                  MouseArea {
                     anchors.fill: parent
                     onClicked: {
                         // Only change state when two other rectangle color is red
                         if (Qt.colorEqual(rect1.color, "red") && Qt.colorEqual(rect2.color, "red"))
                         {
                             Qt.colorEqual(parent.color, "red")?parent.color = "blue" : parent.color="red"
                         }
                     }
                 }
              }
          }
          
          

          By the way, you can also utilize states for this purpose as illustrated below:

          import QtQuick 2.12
          import QtQuick.Window 2.12
          
          Window {
              visible: true
              width: 270
              height: 270
              title: qsTr("Hello World")
          
              Rectangle {
                  id: rect1
                  x: 20; y: 20
                  width: 100; height: 100
                  state:"blueState"
                  MouseArea {
                     anchors.fill: parent
                     onClicked: rect1.state == "redState"?rect1.state="blueState":rect1.state="redState"
                 }
          
                  states: [
                      State {
                         name: "redState";
                         PropertyChanges { target: rect1; color: "red";  }},
                      State {
                         name: "blueState";
                         PropertyChanges { target: rect1; color: "blue";  }
                      }
                  ]
              }
          
              Rectangle {
                  id: rect2
                  x: 150; y: 20
                  width: 100; height: 100
                  state:"blueState"
                  MouseArea {
                     anchors.fill: parent
                     onClicked: rect2.state == "redState"?rect2.state="blueState":rect2.state="redState"
                 }
          
                  states: [
                      State {
                         name: "redState";
                         PropertyChanges { target: rect2; color: "red";  }},
                      State {
                         name: "blueState";
                         PropertyChanges { target: rect2; color: "blue";  }
                      }
                  ]
              }
          
              Rectangle {
                  id: rect3
                  x: 85; y: 150
                  width: 100; height: 100
                  state:"blueState"
          
                  MouseArea {
                     anchors.fill: parent
                     onClicked: {
                         // Only change state when two other rectangle color is red
                         if (rect1.state == "redState" && rect2.state=="redState")
                         {
                             rect3.state == "redState"?rect3.state="blueState":rect3.state="redState"
                         }
                     }
                 }
          
                  states: [
                      State {
                         name: "redState";
                         PropertyChanges { target: rect3; color: "red";  }},
                      State {
                         name: "blueState";
                         PropertyChanges { target: rect3; color: "blue";  }
                      }
                  ]
              }
          }
          
          
          K Offline
          K Offline
          kkai295
          wrote on last edited by
          #4

          @mfatih oh! the state helps alot! THanks!!!

          mfatihM 1 Reply Last reply
          0
          • K kkai295

            @mfatih oh! the state helps alot! THanks!!!

            mfatihM Offline
            mfatihM Offline
            mfatih
            wrote on last edited by
            #5

            @kkai295 Glad, it helped.
            As @GrecKo pointed out (good point, thanks), it may be a little bit verbose for simple cases, but for more complex cases, I believe states might be useful.

            You can find more about me and my post at www.yazilimperver.com

            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