【问题标题】:qt Quick Button palette does not works with Qt6qt 快速按钮调色板不适用于 Qt6
【发布时间】:2021-04-26 22:22:20
【问题描述】:

这在 Qt 5.15 之前一直有效

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

但不是 Qt6

【问题讨论】:

  • 显示进口
  • 导入 QtQuick 导入 QtQuick.Window 导入 QtQuick.Controls

标签: qt qtquick2 qt-quick qtquickcontrols2


【解决方案1】:

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

在 Qt5 中,解决方案不是混淆导入(例如使用命名空间),但在 Qt6 中删除了第一组,因此现在只使用 Qt Quick Controls 2,并且该组具有不同的设置 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
    }
}

【讨论】:

  • 这看起来不太好。如果我们必须使用矩形来改变背景,那么 Button 的用途是什么。
  • @Harris Button是一个Item,它使用其他item来定义它的外观,一个矩形只绘制矩形但没有信号和按钮的其他方面。你只需要修改 contentItem 或 background,你不需要两者。
猜你喜欢
  • 1970-01-01
  • 2020-03-16
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2019-01-16
  • 1970-01-01
  • 2011-10-29
相关资源
最近更新 更多