MyButton::MyButton( const QString & text, QWidget *parent) : QPushButton(text, parent){ this->setCheckable(true); this->setAutoFillBackground(true);}void MyButton::paintEvent ( QPaintEvent * event ){ QBrush brush; if(this->isChecked() ) brush.setColor(QColor(255, 228, 159, 255)); else brush.setColor(QColor(129, 114, 81, 255)); QPalette palette; palette.setBrush(QPalette::Active, QPalette::Button, brush); palette.setBrush(QPalette::Inactive, QPalette::Button, brush); this->setPalette(palette); ... QPushButton::paintEvent(event);}
myPalette = pushButton->palette();myPalette.setColor( pushButton->backgroundRole(), myColor );pushButton->setPalette( myPalette );
myPalette = palette();myPalette.setColor( backgroundRole(), isChecked() ? myColor1 : myColor2 );setPalette( myPalette );
void MyButton::paintEvent ( QPaintEvent * event ){ ... setIcon(QIcon(...)); ...};
void MyButton::paintEvent ( QPaintEvent * event ){ static const QIcon icon(...); ... setIcon(icon); ...};
// Файл MyButton.hclass MyButton : public QPushButton{public: static const QIcon icon; ....}// Файл MyButton.cppconst QIcon MyButton::icon = QIcon(...);
void MyButton::nextCheckState(){ QPushButton::nextCheckState(); QPalette myPalette = palette(); myPalette.setColor( backgroundRole(), isChecked() ? color1 : color2 ); setPalette( myPalette );}