2

Using Qt 5.2.1

Is it possible to set a QSlider (doesn't matter if it's horizontal or vertical) to read-only that is user cannot change the value of the slider but only use it as an indicator of some sort? I was unable to find anything in the Qt documentation or the Qt Designer.

Example for application: displaying a binary state of some sort in the GUI (in my case is the emergency stop on or off).

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161

4 Answers4

7

AFAIK such feature is not available in the QSlider implementation.

However, you can create your own class deriving from QSlider and implement the desired behavior by overwriting mousePressEvent, mouseReleaseEvent, mouseMoveEvent, keyPressEvent and keyReleaseEvent and only call the respective parent implementation if the readOnly property is set to false.

Luckily, such an implementation is already available in kalarm, so have a look at it: http://api.kde.org/4.6-api/kdepim-apidocs/kalarm/lib/html/slider_8cpp_source.html

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • This is the correct answer though in my case I'm (sadly) restricted to using UI files and I have to use only what is generically provided by Qt thus this is not possible. Disabling the QSlider (as suggested by @t3ft3l--i) did the trick although I do believe that disabling is not the "proper" solution since here we are indeed talking about adding a new feature to a component so creating a new class is the right thing to do. – rbaleksandar May 29 '15 at 11:23
  • From reading the Qt documentation what disable basically does is deactivate all signals of this component...At least when it comes to interaction by the user. Internally the state can still be changed. – rbaleksandar May 29 '15 at 11:24
  • @rbaleksandar You can still use UI files with custom widget implementations, have a look at http://doc.qt.io/qt-4.8/designer-using-custom-widgets.html – m.s. May 29 '15 at 16:23
  • The link if not valid anymore. – Zheng Qu Jan 12 '18 at 10:08
2

Maybe a QProgressBar would be more suitable since users know it as "read only" and "shows how much has been done".

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

Following kuba ubar's second approach -

Suppose the object name of your slider is horizontalSlider. Then the code should be

// getting the palette of the slider
QPalette _sliderPalette = ui->horizontalSlider->palette();

// changing the colorGroup of that palette
_sliderPalette.setCurrentColorGroup(QPalette::Active);

// setting the changed palette to the slider 
ui->horizontalSlider->setPalette(_sliderPalette);
Yousuf Azad
  • 414
  • 1
  • 7
  • 17
0

One simple solution would be to install an event filter on the slider that consumes all mouse, focus and keyboard events. You'd also need to make the slider have a Qt::NoFocus policy. Such an event filter would be universal and could be used with any control widget.

An alternative would be to disable the widget, and style it so that the disabled and enabled palette are the same. This might not work with some of the platform styles, though, and would need experimental verification before you commit to it.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313