1

i wrote a simple class that derives QPushButton, and tried to apply stylesheet on it. but it didn't work. I read the qt doc, but i could not find the point. Can any one help me out?

class Button : public QPushButton
{
Q_OBJECT

public:
    Button(QWidget * parent = NULL);
    ~Button();
    protected:
    void keyPressEvent(QKeyEvent * event);

};


Button * btn = new Button(rootframe);
// I tried the following ways, all NG.
btn->setStyleSheet("background: white; color: blue;");
btn->setStyleSheet("QPushButton{background: white; color: blue;}");
btn->setStyleSheet("Button {background: white; color: blue;}");

Thanks..

Chris
  • 17,119
  • 5
  • 57
  • 60
kumo
  • 227
  • 1
  • 10
  • Does it do anything at all? `btn->setStyleSheet("background-color: white; color: blue");` should work. – pezcode Mar 21 '12 at 15:38

1 Answers1

2

From the Qt Style Sheets Reference:

Warning: If you only set a background-color on a QPushButton, the background may not appear unless you set the border property to some value. This is because, by default, the QPushButton draws a native border which completely overlaps the background-color.

This should work

btn->setStyleSheet("background-color: white; color: blue; border: none");

It is advisable to check the QPushButton stylesheets example.

pnezis
  • 12,023
  • 2
  • 38
  • 38