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. [SOLVED] Behavior seems to have no effect
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Behavior seems to have no effect

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 1.4k 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.
  • U Offline
    U Offline
    uchan
    wrote on last edited by
    #1

    Hello,

    I'm learning QML, and I have a question about "Behavior".
    I wrote following code and the code doesn't work as expected.
    I expect that colors change slowly when I press and release a mouse button, but in fact colors change quickly.

    When I comment out the first Behavior line and uncomment the second (use "Behavior on" syntax), the problem is solved.
    What is wrong with the first Behavior line?

    @
    import QtQuick 2.0

    Rectangle {
    id: rect
    width: 200
    height: 200

    MouseArea {
        anchors.fill: rect
        onPressed: rect.color = "black"
        onReleased: rect.color = "white"
    }
    
    // This line seems to have no effect
    Behavior { ColorAnimation { property: color; duration: 1000 } }
    
    // This line works as expected
    //Behavior on color { ColorAnimation { duration: 1000 } }
    

    }
    @

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

      The one on line 15 does not know what to base the bahaviour on. QML needs to know what property does the Behavior apply to. That's why line 18 works.

      Alternatively you can use this:
      @
      ColorAnimation on color { duration: 1000 }
      // Or this:
      ColorAnimation { property: color; duration: 1000 }
      @

      (Z(:^

      1 Reply Last reply
      0
      • U Offline
        U Offline
        uchan
        wrote on last edited by
        #3

        Thanks sierdzio.

        I've got it.
        I must specify a target of Behavior with "on" syntax or use only ColorAnimation.

        In fact, my question came from the description of Behavior in "Getting Started Programming with Qt Quick.":http://qt-project.org/doc/qt-5.1/qtdoc/gettingstartedqml.html#decorating-the-text-editor
        Following code is in the article:
        @
        In TextEditor.qml:

        Behavior{
            NumberAnimation{property: "rotation";easing.type: Easing.OutExpo }
        }
        

        @
        This code doesn't include "on" syntax, so this will not work, won't it?

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

          Please notice there is a difference between your example and the one taken from the tutorial: the tutorial is passing property name as a string, and you are not.

          I don't have QML engine VM in my head, so I can't say for sure whether it would work. You can try. Since it's in a tutorial it will probably work :)

          (Z(:^

          1 Reply Last reply
          0
          • U Offline
            U Offline
            uchan
            wrote on last edited by
            #5

            Thanks sierdzio

            Let me summarize my experiment.

            work fine:
            Behavior on color {ColorAnimation { duration: 1000 }}

            doesn't work:
            Behavior {ColorAnimation { property: "color"; duration: 1000 }}
            Behavior {ColorAnimation { property: color; duration: 1000 }}
            ColorAnimation on color { duration: 1000 }
            ColorAnimation { property: "color"; duration: 1000 }
            ColorAnimation { property: color; duration: 1000 }

            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