简体   繁体   English

更改 QCheckBox 的背景颜色

[英]Changing Background color of QCheckBox

I want to have a checkbox with the following style:我想要一个具有以下样式的复选框:

For that purpose I have created the follow stylesheet:为此,我创建了以下样式表:

QCheckBox { background-color : cyan; }
QCheckBox::indicator { width : 20px; height : 20px; }

sadly the result was not was I expected, here is what I get:遗憾的是,结果出乎我的意料,这是我得到的:

在此处输入图片说明

But I want to have all background with the color cyan.但我希望所有背景都带有青色。 I have checked the Qt Documentation About StyleSheet and have also also checked some old questions, but none of the has served me.我检查了关于样式表Qt 文档,还检查了一些旧问题,但没有一个对我有用。 When I added QCheckBox::indicator{ background-color : cyan; }当我添加QCheckBox::indicator{ background-color : cyan; } QCheckBox::indicator{ background-color : cyan; } the button became all cyan (like on the first image on the state unchecked) on checked and unchecked states. QCheckBox::indicator{ background-color : cyan; }按钮成了所有青色(如对国家未选中第一个图像)上选中和未选中状态。 Neither adding other proprieties like border helped me.添加诸如border类的其他属性都没有帮助我。

I also tried to use QPalette instead, but never changed.我也尝试改用 QPalette,但从未改变。

Here is my code:这是我的代码:

    QWidget* w = new QWidget();

    QCheckBox* cb = new QCheckBox(w);

    cb->setFixedSize(20, 20);
    cb->setStyleSheet(QString("QCheckBox { background-color : cyan; }") + '\n' +
                      "QCheckBox::indicator { width : 20px; height : 20px; border : }");
    w->show();

EDIT 28/09/2020编辑 28/09/2020

I have recently figured out that the style of the application has influence on it.我最近发现应用程序的风格对其有影响。 I was using the Breeze style, changing to Fusion style made it work as expected.我使用的是 Breeze 风格,更改为 Fusion 风格使其按预期工作。 The only problem is that it requires change the theme of the application itself.唯一的问题是它需要更改应用程序本身的主题。 Anyway, here is the code sample in case it helps anyone:无论如何,这是代码示例,以防它对任何人有帮助:

#include <QApplication>
#include <QWidget>
#include <QCheckBox>


int main(int argc, char** argv)
{
    QApplication a(argc, argv);
    qApp->setStyle(QStyleFactory::create("Fusion"));
    QWidget* w = new QWidget();

    QCheckBox* cb = new QCheckBox(w);

    cb->setFixedSize(20, 20);
    cb->setStyleSheet(QString("QCheckBox { background-color : cyan; }") + '\n' +
                      "QCheckBox::indicator { width : 20px; height : 20px; border : }");
    w->show();
}

Probably大概

QCheckBox::indicator:unchecked {background-color : cyan;});

is what you wanted to see.是你想看到的。

Edit: Too late编辑:为时已晚

I did not solve my problem using the stylesheet commands, I actually think that it is not possible SOURCE .我没有使用样式表命令解决我的问题,我实际上认为SOURCE是不可能的。

Anyway, I was I able to achieve my goals using one image for the checked state and another one for the unchecked state as follow.无论如何,我能够使用一张用于选中状态的图像和另一幅用于未选中状态的图像来实现我的目标,如下所示。

    QWidget* w = new QWidget();

    QCheckBox* cb = new QCheckBox(w);

    cb->setFixedSize(20, 20);
    cb->setStyleSheet("QCheckBox::indicator { width : 20; height : 20; }\n"
                      "QCheckBox::indicator::unchecked { image : url(/path/**/unchecked.png); }\n"
                      "QCheckBox::indicator::checked { image : url(/path/**/checked.png); }");
    w->show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM