I'd give QStyle::styleHint(QStyle::SH_Slider_AbsoluteSetButtons) a try:
Which mouse buttons cause a slider to set the value to the position clicked on.
I'd give QStyle::styleHint(QStyle::SH_Slider_AbsoluteSetButtons) a try:
Which mouse buttons cause a slider to set the value to the position clicked on.
J-P Nurmi
To extend a bit, see Qt Centre wiki: Proxy style and the following example:
Qt Code:
class CustomStyle : public ProxyStyle { public: MyStyle(const QString& baseStyle) : ProxyStyle(baseStyle) { } int styleHint(QStyle::StyleHint hint, const QStyleOption* option = 0, const QWidget* widget = 0, QStyleHintReturn* returnData = 0) const { return (Qt::LeftButton | Qt::MidButton | Qt::RightButton); return ProxyStyle::styleHint(hint, option, widget, returnData); } };To copy to clipboard, switch view to plain text mode
J-P Nurmi
mentalmushroom (2nd September 2011)
Take a look at
Qt Code:
QStyle::sliderValueFromPosition ( int min, int max, int position, int span, bool upsideDown = false )To copy to clipboard, switch view to plain text mode
So:
Qt Code:
}To copy to clipboard, switch view to plain text mode
Luc4 (20th June 2010)
By combining all of the above answers, I have applied like the following way, and it worked:
1. Install an event filter for QSlider m_slider (QSlider* m_slider = new QSlider())
Qt Code:
{ ui.setupUi(this); // m_Slider is a QSlider* m_slider->installEventFilter(this); //... }To copy to clipboard, switch view to plain text mode
2. In the event filter change position of Slider's handle:
Qt Code:
{ { m_slider->setValue(QStyle::sliderValueFromPosition(m_slider->minimum(), m_slider->maximum(), mouseEvent->x(), m_slider->width())); } return false; }To copy to clipboard, switch view to plain text mode
astodolski (10th December 2013)
Slick! That worked great. Just what I was looking for![]()
just a note in case someone stumbles across this:
using the widget _width_ as _span_ parameter in QStyle::sliderValueFromPosition() is probably not going to return correct values.
for example in a horizontal slider the handle has a particular width, thus the end positions for the min/max values are reduced at least by half the width of the handle - that's just why the parameter is actually called span - as it needs to reflect the space that is available for sliding the handle center, not the total width of the slider widget (of course the handle needs to fit within the total width of the widget when set to min/max).
it is also possible for a style/proxy style to change this "sliding span" further by modifying pixel metric values, thus this also might need to be accounted, and since this method is static it of course can't know about all the stuff like the current style handle width/height and/or pixel metrics (if you look at the sources of QStyle, this method does just some simple math, that doesn't deal with the actual geometry of the slider).
Bookmarks