Results 1 to 10 of 10

Thread: QSlider step customize

  1. #1
    Join Date
    Sep 2007
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QSlider step customize

    Hello,

    How can I make my slider handle to move on the place where I had clicked (on the slider) instead to move with fixed step?

    Thanks!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSlider step customize

    Override mousePressEvent and calculate where the click occurred within the slider(the best is to get that in %). Next you must set the corresponding value.

  3. #3
    Join Date
    Sep 2007
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSlider step customize

    lol I have no idea how to convert the mouse position from pixels to slider position...

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSlider step customize

    Let's say the user clicks at (100, 50), and the slider geometry is (30, 40, 200, 60).
    Then the slider width is 200-30=170px.
    Now, clicking at 100 pixels(relative to the slider) means the knob should be moved at 58.82% from the slider value.
    So if the slider value goes from 1 to 440, then you should use QSlider:setValue(440*58.82/100).
    This should position the know approximately(due to rounding errors) under the mouse cursor.

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSlider step customize

    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

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSlider step customize

    To extend a bit, see Qt Centre wiki: Proxy style and the following example:
    Qt Code:
    1. class CustomStyle : public ProxyStyle
    2. {
    3. public:
    4. MyStyle(const QString& baseStyle) : ProxyStyle(baseStyle) { }
    5.  
    6. int styleHint(QStyle::StyleHint hint, const QStyleOption* option = 0, const QWidget* widget = 0, QStyleHintReturn* returnData = 0) const
    7. {
    8. if (hint == QStyle::SH_Slider_AbsoluteSetButtons)
    9. return (Qt::LeftButton | Qt::MidButton | Qt::RightButton);
    10. return ProxyStyle::styleHint(hint, option, widget, returnData);
    11. }
    12. };
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    mentalmushroom (2nd September 2011)

  8. #7
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: QSlider step customize

    Take a look at
    Qt Code:
    1. 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:
    1. void CustomSlider::mouseReleaseEvent(QMouseEvent * event) {
    2. setValue(QStyle::sliderValueFromPosition(minVal,maxVal,event->x(),width(),0));
    3. }
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to BatteryCell for this useful post:

    Luc4 (20th June 2010)

  10. #8
    Join Date
    Mar 2009
    Posts
    5
    Thanked 1 Time in 1 Post

    Default Re: QSlider step customize

    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:
    1. myGUI::myGUI(QWidget *parent)
    2. : QWidget(parent),
    3.  
    4. {
    5. ui.setupUi(this);
    6.  
    7. // m_Slider is a QSlider*
    8. m_slider->installEventFilter(this);
    9.  
    10. //...
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    2. In the event filter change position of Slider's handle:
    Qt Code:
    1. bool myGUI::eventFilter(QObject* watched, QEvent* event)
    2. {
    3. if (watched == m_slider && event->type() == QEvent::MouseButtonRelease )
    4. {
    5. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
    6. m_slider->setValue(QStyle::sliderValueFromPosition(m_slider->minimum(), m_slider->maximum(), mouseEvent->x(), m_slider->width()));
    7. }
    8.  
    9. return false;
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to alexi_nedo for this useful post:

    astodolski (10th December 2013)

  12. #9
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: QSlider step customize

    Slick! That worked great. Just what I was looking for

  13. #10
    Join Date
    Dec 2013
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QSlider step customize

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.