简体   繁体   English

更改 QCheckBox 指示器矩形颜色

[英]Changing QCheckBox indicator rectangle color

I'm trying to change only the color of QCheckBox indicator rectangle.我正在尝试仅更改QCheckBox指示符矩形的颜色。

Currently I succeed to draw the right and the bottom line of the rectangle.目前我成功绘制了矩形的右边和底线。 Probably I'm doing something wrong here.可能我在这里做错了什么。

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

CheckBoxWidget.cpp CheckBoxWidget.cpp

CheckBoxWidget::CheckBoxWidget(QObject *poParent)
    : QItemDelegate(poParent)
{
}

void CheckBoxWidget::drawCheck( QPainter *painter,
                                const QStyleOptionViewItem &option,
                                const QRect & rect,
                                Qt::CheckState state) const
{
    QRect oCheckBoxRect =
            QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &option);

    painter->setPen(Qt::white);
    painter->drawRect(oCheckBoxRect);

    QItemDelegate::drawCheck(painter, option, oCheckBoxRect, state);
}

CheckBoxWidget.h CheckBoxWidget.h

class CheckBoxWidget : public QItemDelegate
{
    Q_OBJECT

public:
    CheckBoxWidget(QObject *poParent = 0);
    virtual ~CheckBoxWidget();

protected:
    virtual void drawCheck( QPainter *painter,
                            const QStyleOptionViewItem &option,
                            const QRect &,
                            Qt::CheckState state) const;
};

Any suggestions?有什么建议么?

There is no need to create a custom delegate for this. 无需为此创建自定义委托。 You could use stylesheets in order to change the color of the checkbox or any othe widget as you wish. 您可以使用样式表来根据需要更改复选框或任何其他控件的颜色。 The following stylesheet will set a gray 3px border to the checkbox rectangle. 以下样式表将为复选框矩形设置3px的灰色边框。

QCheckBox::indicator {
    border: 3px solid #5A5A5A;
    background: none;
}

In order to set the stylesheet you could either use the Qt Designer or the setStylesheet function. 为了设置样式表,您可以使用Qt DesignersetStylesheet函数。

In order to set a specific color all of the following are valid: 为了设置特定的颜色,以下所有内容均有效:

border: 3px solid #5A5A5A;
border: 3px solid red;
border: 3px solid rgb(255, 120, 100);
border: 3px solid rgba(255,120,100, 50); // For alpha transparency

The simplest way I have been able to figure out is use a QApplication color palette to help change the checkbox indicator color. 我能够弄清的最简单的方法是使用QApplication调色板来帮助更改复选框指示器的颜色。 This makes it so you don't have to override the entire checkbox widget with all the states with QStyle. 这样一来,您就不必用QStyle的所有状态覆盖整个复选框小部件。

This is the type of code that did what I needed 这是执行我需要的代码的类型

QPalette newPallete = myWidget->palette();
newPallete.setColor(QPalette::Active, QPalette::Background, myWidget->palette().text())
myWidget->setPalette(newPallete)

See this other example with something very similar to styling buttons: Qt5 - setting background color to QPushButton and QCheckBox 看到另一个与样式按钮非常相似的示例: Qt5-将背景色设置为QPushButton和QCheckBox

The solution of @pnezis has the issue that the tick isn't shown anymore. @pnezis 的解决方案的问题是不再显示刻度线。

I fixed this by creating a custom widget, in which I put a QCheckBox (without a label) and a QLabel next to each other.我通过创建一个自定义小部件来解决这个问题,在其中我将一个 QCheckBox(没有标签)和一个 QLabel 放在一起。

Then, on the QCheckBox, I call然后,在 QCheckBox 上,我调用

checkBox.setStyleSheet('background-color: rgba(255, 90, 90, 0.7);')

which changes the colour of the QCheckBox but still displays the tick.这会更改 QCheckBox 的颜色,但仍显示刻度。

My solution to this problem was to change the border-color of QCheckBox::indicator and then fix the disappearing tick mark by making the background-color of QCheckBox::indicator:checked act as the visual replacement of the tick mark.我对这个问题的解决方案是更改 QCheckBox::indicator 的边框颜色,然后通过使 QCheckBox::indicator:checked 的背景颜色作为刻度线的视觉替换来修复消失的刻度线。

checkBox.setStyleSheet("\
    QCheckBox::indicator { border: 1px solid; border-color: yellow; }\
    QCheckBox::indicator:checked { background-color: green; }\
");

More QCheckBox::indicator:<state>'s can be found here .更多 QCheckBox::indicator:<state> 可以在这里找到。

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

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