Results 1 to 10 of 10

Thread: QSlider step customize

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

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

    Luc4 (20th June 2010)

  3. #2
    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 

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

    astodolski (10th December 2013)

  5. #3
    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

  6. #4
    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
  •  
Qt is a trademark of The Qt Company.