qt Quick Button palette does not works with Qt6

621 Views Asked by At

This used to work till Qt 5.15

Button {
    text: "Test button"
        
    palette {
        button: "green"
    }
}

But not with Qt6

1

There are 1 best solutions below

2
On

In Qt5 there are 2 groups of items with very common elements as I explain in this post: QML - Cannot assign to non-existent property "style".

In Qt5 the solution was not to confuse the imports (for example use a namespace) but in Qt6 the first group was removed so now only Qt Quick Controls 2 is used and that group has a different way of setting the button style:

import QtQuick
import QtQuick.Controls

Button {
    id: control
    text: qsTr("Button")

    contentItem: Text {
        text: control.text
        font: control.font
        opacity: enabled ? 1.0 : 0.3
        color: control.down ? "#17a81a" : "#21be2b"
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        elide: Text.ElideRight
    }

    background: Rectangle {
        implicitWidth: 100
        implicitHeight: 40
        opacity: enabled ? 1 : 0.3
        border.color: control.down ? "#17a81a" : "#21be2b"
        border.width: 1
        radius: 2
    }
}