Mattia
Mattia

Reputation: 155

Issue changing a QPushButton style in Qt

I have created a QPushButton in Qt without applying any style, so it inherits the style from Windows10, with this result:

Windows Style

Then I wanted to change temporary the color of the button, so I used:

pushButton->setStyleSheet("background-color: rgb(255,220,220)")

getting this result:

Red Style

Already this result does not satisfy me because also the style is slightly different from the original one. Anyway the next step was that the button had to return to the "normal" style when pressed, so I added this command

pushButton->setStyleSheet("background-color: rgb(240,240,240)")

but the result is different from the starting button:

enter image description here

Can you please give me some advice to better manage the style?

Thanks

Upvotes: 0

Views: 5447

Answers (2)

Pavan Chandaka
Pavan Chandaka

Reputation: 12731

Actually when you set background-color alone to QPushButton, The background may not appear unless you set some value for border.

Look here for (List of Stylable Widgets: QPushButton) http://doc.qt.io/qt-5/stylesheet-reference.html

I think in windows 10 for some reason, you are able to see something without even setting border.

But the recommended way is to set some border value.

So try setting border value as said below, and see if it addresses your requirement:

pushButton->setStyleSheet("background-color: rgb(255,220,220);border: none; ")

In the above said link you can find below information:

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.

Upvotes: 1

konakid
konakid

Reputation: 65

Here are some snippets you may find quite similar and helpful.

I had an Update button which I turned into red Cancel button. Once the update action is finished or cancel is pressed, I restored the original color and text.

// Global variables to save off button state
QPalette update_btn_palette_restore;
QString update_btn_text_restore;

....

// Update button is pressed. 
// Save the palette and text.
update_btn_palette_restore = _ui->update_button->palette ();
update_btn_text_restore = _ui->update_button->text ();

// Change the color palette and text
QPalette p=palette();
p.setBrush(QPalette::Button,Qt::red);
_ui->update_button->setPalette(p);
_ui->update_button->setText ("Cancel");

....

// Handler for when either cancel is pressed or update has finished
if(! update_btn_text_restore.isEmpty ()) {
    _ui->update_button->setText (update_btn_text_restore);
    _ui->update_button->setPalette(update_btn_palette_restore);
}

Upvotes: 0

Related Questions