1

I am looking for a way to disable a part of a QSlider, and I mean disable it such that when you drag it it doesn't allow you to go passed a certain value. I know this effect is usually enforced with setMinumum() and setMaximum(), but for UI reasons I want to set the range larger than the actual possible values set by the user.

In other words, I have a set of sliders, which range is normalized [-2,2]. However I want to enforce one of this sliders only to be [-1,1], but for a better visualization I want such slider to have the same physical size as the others, but only let the user move between [-1,1]

Is there such a possibility?

Dan
  • 1,466
  • 1
  • 13
  • 27
  • Can you not restrict that on `mouseMoveEvent`? – Azeem Jun 30 '17 at 16:42
  • @Azeem could you elaborate? I don't see what Event I could restrict with mouseMoveEvent such that it doesn't allow certain Slider positions. Are you thinking on restricting the positions the mouse can move? – Dan Jun 30 '17 at 16:55
  • You've read my mind. :) Yes. Exactly. That's is what I'm thinking. – Azeem Jun 30 '17 at 16:57
  • @Azeem Seems overcomplicated to be. I think it should be possible to disable parts of the slider somehow... – Dan Jun 30 '17 at 16:58
  • Yes. But, I don't see anything relevant in the documentation. BTW, `sliderMoved` may also be used instead of the event and the slider position will also be in it. – Azeem Jun 30 '17 at 17:04
  • 2
    I subclassed `QSlider` and re-implemented the [`sliderChange`](https://doc.qt.io/qt-5/qabstractslider.html#sliderChange) method to *correct* the new value of the slider (to make it fit into the user-specified range). It seems to be working, You can have a look [here](https://pastebin.com/JpycY58D). It is in c++ as I don't have pyqt installed, but the concept should be the same in python. – Mike Jun 30 '17 at 17:33
  • 1
    but I had to use a *hack* to block signals from the object. That's why I think a cleaner way would be re-implementing `mouseMoveEvent` and filter the event at that level to make the slider only work on a specific range. . . – Mike Jun 30 '17 at 17:35
  • 1
    You can try to connect to `valueChanged` and clip the value when necessary. – m7913d Jul 03 '17 at 21:19

0 Answers0