QPushButton visual issue
Asked Answered
F

4

6

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?

Fraud answered 31/7, 2012 at 6:35 Comment(0)
F
3

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.

Fraud answered 31/7, 2012 at 8:46 Comment(4)
You may get the slight difference in behavior - when you have focus on button you may 'press' it by the Space key, but it all depends on what you need. Personally I always liked the GUIs that can be used by keyboard, so bad that developers overlook this feature these days :(.Miramontes
@Kolenda, yes, I'm aware of this. I have a QLineEdit on my form, which the user will use to input his stuff. I will track for Enter hits in this QLineEdit, and force the button press when that happens.Fraud
@Kolenda, that's a good point though, I find it very disappointing not to be able to hit enter to complete the filling, too.Fraud
The screenshot you provided is of Qt Designer, not Qt Creator. Creator is an IDE, which can (but not necessarily) integrate DesignerIvied
S
4

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.

Sanguinary answered 16/3, 2013 at 16:18 Comment(0)
F
3

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.

Fraud answered 31/7, 2012 at 8:46 Comment(4)
You may get the slight difference in behavior - when you have focus on button you may 'press' it by the Space key, but it all depends on what you need. Personally I always liked the GUIs that can be used by keyboard, so bad that developers overlook this feature these days :(.Miramontes
@Kolenda, yes, I'm aware of this. I have a QLineEdit on my form, which the user will use to input his stuff. I will track for Enter hits in this QLineEdit, and force the button press when that happens.Fraud
@Kolenda, that's a good point though, I find it very disappointing not to be able to hit enter to complete the filling, too.Fraud
The screenshot you provided is of Qt Designer, not Qt Creator. Creator is an IDE, which can (but not necessarily) integrate DesignerIvied
L
0

I didn't try but I think you could fix that by setting QPushButton:hover to the same style as QPushButton. There's some default style that makes the button red on hover, you just need to find out which one it is (probably hover) and override it.

Lorilee answered 31/7, 2012 at 7:47 Comment(3)
It doesn't become red on hovering. It becomes so after it's clicked, and remains so until any other control is clicked.Fraud
@SingerOfTheFall, right, then try the QPushButton:focus state.Lorilee
@SingerOfTheFall, or could it be the :default state? Logically not but could be worth a try.Lorilee
M
0

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

I've found parameters like:

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

I haven't tried them but it might be what you need.

Miramontes answered 31/7, 2012 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.