QPushButton color is different than what I am setting in QPalette
-
Hi, I am setting
Qt::blue
as myQPalette::Button
and I am usingQProxyStyle
to set the background ofQRadioBox
indicator toQt::blue
as well. Here is my proxy class:class MyProxyStyle : public QProxyStyle { public: void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget = nullptr) const override { if((element == QStyle::PE_IndicatorCheckBox) || (element == QStyle::PE_IndicatorRadioButton)) { QStyleOptionButton myButtonOption; const QStyleOptionButton* buttonOption = qstyleoption_cast<const QStyleOptionButton*>(option); if(buttonOption) { myButtonOption = *buttonOption; if(myButtonOption.palette.currentColorGroup() != QPalette::Disabled) { myButtonOption.palette.setBrush(QPalette::Base, QBrush(Qt::blue, Qt::SolidPattern)); myButtonOption.palette.setBrush(QPalette::Text, QBrush(Qt::white, Qt::SolidPattern)); } } QProxyStyle::drawPrimitive(element, &myButtonOption, painter, widget); } else { QProxyStyle::drawPrimitive(element, option, painter, widget); } } void drawControl(QStyle::ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget = nullptr) const override { if((element == QStyle::CE_CheckBoxLabel) || (element == QStyle::CE_RadioButtonLabel)) { QStyleOptionButton myButtonOption; const QStyleOptionButton* buttonOption = qstyleoption_cast<const QStyleOptionButton*>(option); if(buttonOption) { myButtonOption = *buttonOption; QWidget* btn = qobject_cast<QWidget*>(myButtonOption.styleObject); QFont fnt = btn->font(); if(myButtonOption.state & State_On) fnt.setItalic(true); else fnt.setItalic(false); myButtonOption.styleObject->setProperty("font", fnt); } QProxyStyle::drawControl(element, &myButtonOption, painter, widget); } else if(element == QStyle::CE_PushButton) { QStyleOptionButton myButtonOption; const QStyleOptionButton* buttonOption = qstyleoption_cast<const QStyleOptionButton*>(option); if(buttonOption) { myButtonOption = *buttonOption; QWidget* btn = qobject_cast<QWidget*>(myButtonOption.styleObject); QFont fnt = btn->font(); if(myButtonOption.state & (State_On | State_Sunken)) { fnt.setItalic(true); myButtonOption.palette.setBrush(QPalette::Button, QBrush(Qt::blue, Qt::SolidPattern)); myButtonOption.palette.setBrush(QPalette::ButtonText, QBrush(Qt::white, Qt::SolidPattern)); } else { fnt.setItalic(false); myButtonOption.palette.setBrush(QPalette::Button, QBrush(Qt::blue, Qt::SolidPattern)); myButtonOption.palette.setBrush(QPalette::ButtonText, QBrush(Qt::white, Qt::SolidPattern)); } myButtonOption.styleObject->setProperty("font", fnt); } QProxyStyle::drawControl(element, &myButtonOption, painter, widget); } else { QProxyStyle::drawControl(element, option, painter, widget); } } };
I am using the fusion style. When I implement this code, this is what I get:
The color of
QPushButton
and ofQRadioButton
's indicator background is very different, even though I am applyingQt::blue
to both. How can make the two colors the same? Or better, Is there any way I can ask Qt not to modify theQPalette::Button
color at all and use the sameQPalette::Button
color to render both pressed and non-pressed states of a button, etc.? (since it used in rendering other things as well such asQComboBox
,QSpinBox
, etc.)I want to implement my own colors to distinguish between pressed and non-pressed push buttons since I find the difference between the pressed and non-pressed button color barely noticeable in the fusion style. I want to provide my own colors but I would like to have consistency in whatever color I provide so that I can confidently choose a single text color (either white or black) for a given background color.