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. Assign Behavior{} to multiple Elements
Qt 6.11 is out! See what's new in the release blog

Assign Behavior{} to multiple Elements

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 416 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
    PyRookie
    wrote on last edited by
    #1

    In the minimal example below I want to avoid duplicating the Behavior{}.
    Is there a way do define a reusable Behavior{} and assign it to myRect and myText?

    import QtQuick
    
    Window {
        width: 300
        height: 120
        visible: true
    
        Rectangle {
            id: myRect
            x: 10
            y: 10
            width: 100
            height: 100
            color: rectMouseArea.pressed ? 'lightgreen' : 'gray'
    
            Behavior on color {
                ColorAnimation {
                    duration: 300
                }
            }
    
            MouseArea {
                id: rectMouseArea
                anchors.fill: parent
            }
        }
    
        Text {
            id: myText
            x: 120
            y: 10
            font.pixelSize: 50
            text: 'foo'
            color: textMouseArea.pressed ? 'red' : 'blue'
    
            // duplicated from myRect :(
            Behavior on color {
                ColorAnimation {
                    duration: 300
                }
            }
    
            MouseArea {
                id: textMouseArea
                anchors.fill: parent
            }
        }
    }
    
    GrecKoG 1 Reply Last reply
    0
    • P PyRookie

      In the minimal example below I want to avoid duplicating the Behavior{}.
      Is there a way do define a reusable Behavior{} and assign it to myRect and myText?

      import QtQuick
      
      Window {
          width: 300
          height: 120
          visible: true
      
          Rectangle {
              id: myRect
              x: 10
              y: 10
              width: 100
              height: 100
              color: rectMouseArea.pressed ? 'lightgreen' : 'gray'
      
              Behavior on color {
                  ColorAnimation {
                      duration: 300
                  }
              }
      
              MouseArea {
                  id: rectMouseArea
                  anchors.fill: parent
              }
          }
      
          Text {
              id: myText
              x: 120
              y: 10
              font.pixelSize: 50
              text: 'foo'
              color: textMouseArea.pressed ? 'red' : 'blue'
      
              // duplicated from myRect :(
              Behavior on color {
                  ColorAnimation {
                      duration: 300
                  }
              }
      
              MouseArea {
                  id: textMouseArea
                  anchors.fill: parent
              }
          }
      }
      
      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @PyRookie you could define the Behavior in a new file or an inline component:

      // VisibleBehavior.qml
      Behavior {
          ColorAnimation {
              duration: 300
              easing.type: Easing.InOutQuad
          }
      }
      
      Rectangle {
          // ...
          color: rectMouseArea.pressed ? 'lightgreen' : 'gray'
          VisibleBehavior on color {}
      }
      
      P 1 Reply Last reply
      1
      • GrecKoG GrecKo

        @PyRookie you could define the Behavior in a new file or an inline component:

        // VisibleBehavior.qml
        Behavior {
            ColorAnimation {
                duration: 300
                easing.type: Easing.InOutQuad
            }
        }
        
        Rectangle {
            // ...
            color: rectMouseArea.pressed ? 'lightgreen' : 'gray'
            VisibleBehavior on color {}
        }
        
        P Offline
        P Offline
        PyRookie
        wrote on last edited by
        #3

        @GrecKo Thanks, this works perfectly. In previous efforts I missed the „on color“ part in the component call.

        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