简体   繁体   中英

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.

在此输入图像描述

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:

在此输入图像描述

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.

try to edit your qss file with another random style to see if it's consistent.

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

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

Edit :

I see that your arrows are still in bad form so I would suggest you two things :

  • style also your up-down arrows with background image property or so (take a screen shot of the desired arrows..or so)

  • forger to style this part via stylesheet and override the "QPaintEvent onFocus handler" by code. Setting the border as green wouldn't be so painful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM