qtbuttonqmlhovertextcolor

How to change the text color of a QML Button when the Button is hovered in Qt6


How can I change the text color of the Button when the Button is hovered? I don't want to draw a rectangle.

Button {
    id:button
    x: 58
    y: 246
    width: 188
    height: 34
    onClicked: classA.newGameWindow()

    text: qsTr("New Game")

    background: Rectangle {
        opacity: enabled ? 1 : 0.3
        color: "transparent"
    }

    font.family: "Inria Serif"
    font.pixelSize: 26

    Text {
       id: new_Game
       color: "white"
       horizontalAlignment: Text.AlignLeft
       verticalAlignment: Text.AlignTop
       wrapMode: Text.Wrap
       font.weight: Font.Normal
    }
}

I tried to use color: button.down but it draws me a rectangle around the text of the Button.


Solution

  • By setting the contentItem of your button:

        Button {
            id: button
            text: "Button"
    
            contentItem: Text {
                text: button.text
                color: button.hovered ? "green" : "red"
            }
    
        }