This is almost impossible to do on Windows. No matter what you try, either style sheet or palette, the button drawing code does its own thing. The most common solution is to derive a new custom class (*ahem*) from QPushButton and override the paintEvent:
// ColorButton.h
#pragma once
#include <QPushButton>
{
Q_OBJECT
public:
virtual ~ColorButton();
void setBackgroundColor
( const QColor & color
);
protected:
protected:
};
// ColorButton.h
#pragma once
#include <QPushButton>
class ColorButton : public QPushButton
{
Q_OBJECT
public:
ColorButton( const QString & text, QWidget * pParent = 0 );
virtual ~ColorButton();
void setBackgroundColor( const QColor & color );
protected:
void paintEvent( QPaintEvent * pEvent );
protected:
QColor background;
};
To copy to clipboard, switch view to plain text mode
// ColorButton.cpp
#include "ColorButton.h"
#include <QStylePainter>
#include <QStyleOptionButton>
#include <QPaintEvent>
, background( Qt::red )
{
}
ColorButton::~ColorButton()
{
}
void ColorButton
::setBackgroundColor( const QColor & color
) {
background = color;
update();
}
{
painter.fillRect( pEvent->rect(), background );
initStyleOption( &option );
if ( !isFlat() )
painter.
drawPrimitive( QStyle::PE_FrameButtonBevel, option
);
if ( isDefault() )
painter.
drawPrimitive( QStyle::PE_FrameDefaultButton, option
);
if ( hasFocus() )
painter.
drawPrimitive( QStyle::PE_FrameFocusRect, option
);
painter.
drawControl( QStyle::CE_PushButtonLabel, option
);
}
// ColorButton.cpp
#include "ColorButton.h"
#include <QStylePainter>
#include <QStyleOptionButton>
#include <QPaintEvent>
ColorButton::ColorButton( const QString & text, QWidget * pParent )
: QPushButton( text, pParent )
, background( Qt::red )
{
}
ColorButton::~ColorButton()
{
}
void ColorButton::setBackgroundColor( const QColor & color )
{
background = color;
update();
}
void ColorButton::paintEvent( QPaintEvent * pEvent )
{
QStylePainter painter( this );
painter.fillRect( pEvent->rect(), background );
QStyleOptionButton option;
initStyleOption( &option );
if ( !isFlat() )
painter.drawPrimitive( QStyle::PE_FrameButtonBevel, option );
if ( isDefault() )
painter.drawPrimitive( QStyle::PE_FrameDefaultButton, option );
if ( hasFocus() )
painter.drawPrimitive( QStyle::PE_FrameFocusRect, option );
painter.drawControl( QStyle::CE_PushButtonLabel, option );
}
To copy to clipboard, switch view to plain text mode
// main.cpp test program
#include <QWidget>
#include <QApplication>
#include <QVBoxLayout>
#include "ColorButton.h"
int main(int argc, char *argv[])
{
window.
setWindowTitle( QString::fromUtf8( "QPushbutton Background" ) );
window.resize( 336, 227 );
ColorButton *button1 = new ColorButton( "Button 1" );
ColorButton *button2 = new ColorButton( "Button 2" );
ColorButton *button3 = new ColorButton( "Button 3" );
layout->addWidget( button1 );
layout->addWidget( button2 );
layout->addWidget( button3 );
button1->setBackgroundColor( Qt::lightGray );
button2->setBackgroundColor( Qt::green );
button3->setBackgroundColor( Qt::transparent );
button1->setCheckable( true );
button1->setChecked( true );
button2->setEnabled( false );
button3->setDefault( true );
window.show();
return app.exec();
}
// main.cpp test program
#include <QWidget>
#include <QApplication>
#include <QVBoxLayout>
#include "ColorButton.h"
int main(int argc, char *argv[])
{
QApplication app( argc, argv );
QWidget window ;
window.setWindowTitle( QString::fromUtf8( "QPushbutton Background" ) );
window.resize( 336, 227 );
ColorButton *button1 = new ColorButton( "Button 1" );
ColorButton *button2 = new ColorButton( "Button 2" );
ColorButton *button3 = new ColorButton( "Button 3" );
QVBoxLayout* layout = new QVBoxLayout( &window );
layout->addWidget( button1 );
layout->addWidget( button2 );
layout->addWidget( button3 );
button1->setBackgroundColor( Qt::lightGray );
button2->setBackgroundColor( Qt::green );
button3->setBackgroundColor( Qt::transparent );
button1->setCheckable( true );
button1->setChecked( true );
button2->setEnabled( false );
button3->setDefault( true );
window.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
This is a simplistic override of QPushButton. It implements only a few of the painting styles that change with the button state. Some of them are automatically done by the primitives (like drawing the checked state or the border of the default button). Button2 is disabled, but the only obvious thing is that the text is grayed out.
Other things probably need to be implemented (like drawing the icon if the button has one). Doing that is left as an exercise for the reader.
Bookmarks