qt 快速按钮调色板不适用于 Qt6

问题描述

这在 Qt 5.15 之前一直有效

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

但不是 Qt6

解决方法

在 Qt5 中有 2 组具有非常常见元素的项目,正如我在这篇文章中所解释的:QML - Cannot assign to non-existent property "style"

在 Qt5 中,解决方案不是混淆导入(例如使用命名空间),但在 Qt6 中第一组被删除,所以现在只使用 Qt Quick Controls 2 并且该组有不同的方式设置 {{3 }}:

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
    }
}