Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set border for QSpinBox when focused

For already existed Qt project I'd like set border for focused widgets through qss-fle. But I faced out with some unexpected result. When I change border of QSpinBox (and QDoubleSpinBox) border will change as I expect but up-button and down-button change too and look ugly.

enter image description here

Here is my style definition (full example available here):

QSpinBox:focus
{
    border-width: 2px;
    border-style: solid;
    border-color: green;
}

My question is: how to change appearance of border and simultaneously preserve appearance of up-button and down-button. Solution what I am looking for shouldn't be cross platform or cross version.

My environment:
- KUbuntu 15.10 (amd64);
- Qt 5.4 (x64).

Update:

Here is one more example with another style:

QSpinBox
{
    border-width: 2px;
    border-style: solid;
    border-color : red;
}

QSpinBox:hover
{
    border-width: 2px;
    border-style: solid;
    border-color: blue;
}

The widget looks like this:

enter image description here

like image 651
Gluttton Avatar asked Feb 02 '16 19:02

Gluttton


1 Answers

When you apply a style sheet to the QSpinBox, this widget is completely painted using the QStyleSheetStyle (this class is not part of the public API).

So you have to either style your spin box completely, including the up/down buttons or not to use the style sheet at all.

That up/down buttons are not separated widgets, so you can't apply a different style to them.

So I suggest to subclass the QSpinBox and reimplement the paintEvent() method. In your paintEvent() method you will just call it's default implementation and than you will draw a rectangle around.

like image 54
Tomas Avatar answered Sep 20 '22 13:09

Tomas