如何更改“ Qt Quick-Control 2 RoundButton”的颜色

PhB*_*Bsa 3 qt qml qtquickcontrols2

任何人都知道如何更改控件“ RoundButton”的颜色,该控件自2.1开始就存在于Qt控件中。

我尝试更改背景,但是如果我在该项目上放置“矩形”,则RoundButton会变成矩形,而我不知道要放置什么。

RoundButton {
    id: roundButton
    x: 243
    y: 244
    width: 20
    height: 20

    text: "ah"
    wheelEnabled: false

    background: Rectangle {
        color: "yellow"
        height: 50
        width: 50
    }
}
Run Code Online (Sandbox Code Playgroud)

Mit*_*tch 5

在Qt 5.10中,您可以使用选用板属性来避免覆盖背景:

import QtQuick 2.9
import QtQuick.Controls 2.3

ApplicationWindow {
    visible: true

    RoundButton {
        id: button
        text: "ah"

        palette.button: "salmon"
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,目前并非所有样式都尊重调色板属性:默认样式和融合样式都可以使用,而Imagine样式则可以使用它可以放置的位置(主要是文本颜色,因为它是基于图像的样式)。

如果您想对特定样式使用哪个调色板角色,最简单的方法是查看源代码:

http://code.qt.io/cgit/qt/qtquickcontrols2.git/tree/src/imports/controls/Button.qml#n77

尽管记录了角色列表:

https://doc.qt.io/qt-5.10/qml-palette.html#qtquickcontrols2-palette