Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QPushButton visual issue

I have two custom-styled QPushButton buttons. Here is the stylesheet for the Ok button:

QPushButton
{ 
    background-color:#9dce2c;
    border-radius:7px;
    border:1px solid #83c41a;
    color:#ffffff;
    font-size:15px;
    font-weight:bold;
    padding:4px 24px;
    text-decoration:none;
}
QPushButton:pressed
{
    border:2px solid black;
}

Now here's what it looks like:

Ok button

which is fine. However, if the button is clicked (gets focus), it starts to look like this:

Ok button focused

Note that slight shadowy rectangle around the text. It looks as if the text is being "selected". When the button loses focus, it starts looking normal again. I suppose it happens because the selected controls get highlighted like this:

enter image description here

However, I want my button to stay unchanged, no matter whether it's focused or not. Is there any way to solve this issue?

like image 597
SingerOfTheFall Avatar asked Jul 31 '12 06:07

SingerOfTheFall


2 Answers

This removes the orange rectangle :

QPushButton:focus {
    outline: none;
}

PS : You should try to add some style to the focus state like a changed background-color so that this state stays "visible" to the user.

like image 161
Mmmh mmh Avatar answered Oct 09 '22 11:10

Mmmh mmh


Found the solution. It turned out to be very simple.

The issue was indeed caused by the button receiving focus. All I needed to do is set the button's focusPolicy attribute to NoFocus. It can be done either in QtDesigner:

enter image description here

or in the code:

ui.okButton->setFocusPolicy(Qt::NoFocus);

After it's done, the clicks on the button will not cause it to get focus, and the appearance will not change.

like image 40
SingerOfTheFall Avatar answered Oct 09 '22 11:10

SingerOfTheFall