1

I'm trying to make a subclass of QSlider that allows the user to insert "bookmarks" so they can remember significant locations on the slider. I've run into a problem painting the tabs on the slider - using QStyle.sliderPositionFromValue, I get a value but it is slightly inaccurate. If I'm on the left side of the slider, the tab is painted too far left, and on the right side it is painted too far right. I believe this is because QSlider.width() returns the width of the whole object, including the small offsets at the left and right. So width() might return 630 pixels, when the length of the slider itself is really 615.

This is the code I'm using to get the pixel position and draw a line across the slider.

pos = QStyle.sliderPositionFromValue(self.minimum(),self.maximum(),sliderIndex,self.width())
painter.drawLine(pos,0,pos,self.height())

I've been looking at the QT Source here starting on line 2699 and it seems like I need to be using the PixelMetric class from QStyle. I've tried using:

self.style().pixelMetric(QStyle.PM_SliderSpaceAvailable)

But that returns 0, which is clearly not the value I need.

I'd appreciate any advice.

Edit: As suggested in the comments, I changed the call to:

self.style().pixelMetric(QStyle.PM_SliderSpaceAvailable, QStyleOption(1,QStyleOption.SO_Slider),self)

This however, returns -14, which also doesn't match for the value of the offsets (I tried using self.width()-14 but the offset remains.

demonplus
  • 5,613
  • 12
  • 49
  • 68
The Bearded Templar
  • 671
  • 1
  • 7
  • 16
  • I think you need to pass `self` as the third parameter of `pixelMetric` as well as a `QStyleOptionSlider` for the `option` parameter. Otherwise, `pixelMetric` returns 0. – deGoot Mar 19 '14 at 00:39
  • I changed my call to `self.style().pixelMetric(QStyle.PM_SliderSpaceAvailable,QStyleOption(1,QStyleOption.SO_Slider),self)` and it now returns -14, which doesn't make sense to me. Thanks for the reply. – The Bearded Templar Mar 19 '14 at 17:52

0 Answers0