[Qt-interest] QPalette

Girish Ramakrishnan girish at forwardbias.in
Tue May 5 06:14:13 CEST 2009


Alvaro Sánchez wrote:
> Hi all,
> 
>    I'm trying to change the button color (no border) using the below code:
> 
>    QPalette palette = ui.pushButton->palette();
>    palette.setColor( QPalette::Normal, QPalette::Button, QColor(Qt::red) );
>    ui.pushButton->setPalette(palette);
> 
> 
> 
>    ui.pushButton->setAutoFillBackground(true);
> 
>    and the result is wrong, the pushButton border is red instead of the
> button, does anybody know how to resolve it?
> 

This cannot be resolved, read up the stylesheet documentation as to why.
http://doc.trolltech.com/4.5/stylesheet.html.

>    If I use QStyleSheet to define a "background-color: red" to apply the
> pushButton, the result is correct, but my memory grows up, does anybody
> know why is happening?
> 
> #include "ui_testqt.h"
> 
> class testqt : public QDialog
>  {
>      Q_OBJECT
> 
>  public:
>      testqt(QWidget *parent = 0);
>   
> 
>      void Refresh();
> 
>  private slots:
>      void on_pushButton_pressed();    
>      void closeEvent (QCloseEvent *);
> 
> 
>  private:
>      Ui_Dialog ui;
> 
>      bool bStatus;
> 
>     
>  };
> 
> 
> include "testqt.h"
> 
> testqt::testqt(QWidget *parent)
>      : QDialog(parent)
>  {
>      QString qtstylesheet = "background-color: yellow";
>   
>      ui.setupUi(this);
>      bStatus = false;
>  }
> 
> void testqt::on_pushButton_pressed()
> {
>    bStatus = !bStatus;  
> }
> 
> void testqt::Refresh()
> {
>     QString qtstylesheet = "background-color: yellow";
>     QString qtstylesheet2 = "background-color: red";  
>   
>    if (bStatus)
>    {     
>       ui.pushButton->setStyleSheet(qtstylesheet);
>    }
>    else
>    {
>       ui.pushButton->setStyleSheet(qtstylesheet2);
>    }
> 

Seems complex. All you need is the following stylesheet:
setStyleSheet("QPushButton { background-color: yellow; }
QPushButton:pressed { background-color: red }");

The correct usage is in the documentation above.

Girish



More information about the Qt-interest-old mailing list