Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Adding behavior prevents color change of rectangle



  • I have a simple rectangle (below) whose color depends on a function. It works great, changing between 3 colors depending on properties of the overall item.

    In order to ease the transition between colors I added a behavior as shown below. However, once I add the behavior the color is frozen (does not change) when the variables change, and no error is shown.

    What is wrong?

    Rectangle {
        id: rectItemBackground
        color: backgroundColor()
        implicitWidth:  parent.width  -rectRightItemBackground.width/2
        implicitHeight: itemHeightByDepth(_depth)
    
        Behavior on color {
                NumberAnimation { duration: 1000 }
            }
    
    }
    
    function backgroundColor() {
        if (_selected) return gui.colorRGBString(GUIConstants.EColorElement_BackgroundSelected);
        if (_onSelectedPath) return gui.colorRGBString(GUIConstants.EColorElement_BackgroundPartialSelected);
        return gui.colorRGBString(GUIConstants.EColorElement_Background);
    }


  • Seems to be an error in the docs (in an example). Once I used ColorAnimation instead of NumberAnimation it worked.



  • Seems to be an error in the docs (in an example). Once I used ColorAnimation instead of NumberAnimation it worked.


Log in to reply