Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a QSlider read-only?

Tags:

qt

qt5

qslider

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).

like image 477
rbaleksandar Avatar asked Dec 03 '25 23:12

rbaleksandar


2 Answers

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

like image 131
m.s. Avatar answered Dec 06 '25 16:12

m.s.


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

like image 43
Aaron Digulla Avatar answered Dec 06 '25 16:12

Aaron Digulla