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. State is not getting executed in the QML program?

State is not getting executed in the QML program?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 550 Views 2 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.
  • X Offline
    X Offline
    xpress_embedo
    wrote on last edited by
    #1

    Hello Everyone,
    I am new to Qt and started learning QML, I am trying to do a simple thing.
    Whenever the mouse is placed over the rectangle, color change to green and then back to red. The code is as below.

    import QtQuick 2.9
    import QtQuick.Window 2.2
    Window {
        visible: true
        width: 400
        height: 400
        title: qsTr("Behavior with State Demo")
        Rectangle {
             id: rect
             width: 200
             height: 200
             anchors.centerIn: parent
             // color: "red"
    
    //         Behavior on color {
    //             ColorAnimation {}
    //         }
    
             MouseArea {
                 id: mouser
                 anchors.fill: parent
                 hoverEnabled: true
             }
    
             states: [
                 State {
                     name: "GreenState"
                     when: mouser.containsMouse
                     PropertyChanges {
                         target: rect
                         color: "green"
                     }
                 },
                 State {
                     name: "RedState"
                     when: !mouse.containsMouse
                     PropertyChanges {
                         target: rect
                         color: "red"
                     }
                 }
             ]
             onColorChanged: console.log("Color Changed " + color )
        }
    }
    
    

    The console logs are as below.

    qml: Color Changed #008000
    qml: Color Changed #ffffff
    qml: Color Changed #008000
    qml: Color Changed #ffffff
    qml: Color Changed #008000
    qml: Color Changed #ffffff
    

    So the problem is that "RedState" is not getting executed.
    Can anyone please help me, what is the problem in the above program.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      @xpress_embedo said in State is not getting executed in the QML program?:

      when: !mouse.containsMouse

      Copy & Paste the following line in "RedState". It works.

      when: !mouser.containsMouse

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • S Offline
        S Offline
        shaan7
        wrote on last edited by
        #3

        As @dheerendra pointed out, you made a typo. It should be mouser instead of mouse in RedState.

        1 Reply Last reply
        0
        • X Offline
          X Offline
          xpress_embedo
          wrote on last edited by
          #4

          Thanks @dheerendra , a silly mistake done by me.
          Instead of using mouser I used mouse, and unable to identify this mistake.
          Thanks for solution.

          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