QPushButton Appearance

I have two custom 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 is what it looks like:

Ok button

what well. However, if the button is pressed (gets focus), it starts to look like this:

Ok button focused

Notice that there is a slightly shadowed rectangle around the text. It looks like the text is "selected." When the button loses focus, it starts to look normal again. I assume this happens because the selected controls are highlighted as follows:

enter image description here

However, I want my button to stay the same, regardless of whether it is focused or not. Is there any way to solve this problem?

+6
source share
4 answers

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

The problem was caused by the focus button. All I had to do was set the focusPolicy attribute to NoFocus . This can be done either in QtDesigner:

enter image description here

or in code:

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

After that, clicks on the button will not cause focus, and the appearance will not change.

+3
source

This removes the orange rectangle:

 QPushButton:focus { outline: none; } 

PS: You should try to add some style to the focus state, as a changed background color, so that this state remains "visible" to the user.

+3
source

I have not tried it, but I think you could fix it by setting QPushButton:hover in the same style as QPushButton . There's some kind of default style that makes the button red on hover, you just need to figure out which one (probably hover ) and override it.

0
source

Here: http://doc.qt.io/qt-5/stylesheet-examples.html

I found options like:

 "selection-color: yellow;" "selection-background-color: blue;" 

I have not tried them, but maybe what you need.

0
source

Source: https://habr.com/ru/post/921736/


All Articles