Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPushButton color is different than what I am setting in QPalette

QPushButton color is different than what I am setting in QPalette

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpaletteqstyle
1 Posts 1 Posters 244 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by CJha
    #1

    Hi, I am setting Qt::blue as my QPalette::Button and I am using QProxyStyle to set the background of QRadioBox indicator to Qt::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:

    Button_Color.PNG

    The color of QPushButton and of QRadioButton's indicator background is very different, even though I am applying Qt::blue to both. How can make the two colors the same? Or better, Is there any way I can ask Qt not to modify the QPalette::Button color at all and use the same QPalette::Button color to render both pressed and non-pressed states of a button, etc.? (since it used in rendering other things as well such as QComboBox, 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.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved