PDA

View Full Version : QSlider mid position



buster
26th January 2020, 08:41
13327
I want a slider with the mid position as default. I can set the slider to the mid position, but I cannot find a way to set the grove colour to grey when the slider is in the mid position. What I am trying to achieve is for the blue colour to stretch out left/right from the mid position, instead of from the leftmost position. I presume that this is a common enough issue for a solution to excist?

d_stranz
26th January 2020, 17:21
I presume that this is a common enough issue for a solution to exist?

Well, actually, no. The out-of-the-box slider only fills half of the trough, either from the minimum up to the current slider position, or from the maximum down (if invertedAppearance() is set). There doesn't appear to be any way of filling both sides of the trough using the standard widget.

Drawing of complex widgets is deeply embedded in the Qt implementation and depends on the platform. You could derive your own slider from QSlider and reimplement the paintEvent() method, but I invite you to look at the Qt source code for QSlider::paintEvent(), and follow that into the call to QStyle::drawComplexControl(). The specific QStyle derived class which is actually used for drawing depends on the platform. So for example, in QWindowsStyle::drawComplexControl() there are over 215 lines of code just to draw a slider.

You might be better off trying to find something online (like at linux-apps.com (https://www.linux-apps.com/browse/cat/252/order/latest/) or qinclude.org (https://inqlude.org/)) or by studying the QStyle documentation.

ChrisW67
27th January 2020, 06:09
You could apply a default stylesheet and monitor the range change and value change slots and apply a different stylesheet when the mid-value is reached. The important parts of the style sheet might look like this:


QSlider::add-page:vertical { background: blue; }
QSlider::sub-page:vertical { background: blue; }

when off-centre, and this when centred (probably with some tolerance around centre):


QSlider::add-page:vertical { background: grey; }
QSlider::sub-page:vertical { background: grey; }

You would probably want to style the rest of the slider; see https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qscrollbar
You need to reapply the entire stylesheet each time.

d_stranz
27th January 2020, 17:56
Maybe I misunderstood the OP's question. I assumed he wanted the background color to be the same on both sides no matter where the slider was positioned.

buster
30th January 2020, 17:14
13328
The stylesheet idea worked: I got rid of the blue grove by using the following stylesheet;
QSlider::groove:horizontal {
border: 1px solid #999999;
height: 20px;
background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);
margin: 2px 0;
}

QSlider::handle:horizontal {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);
border: 1px solid #5c5c5c;
width: 30px;
margin: -2px 0px;
}
Thanks for the advise.