Results 1 to 2 of 2

Thread: Use user-specifying QSlider step !

  1. #1
    Join Date
    Sep 2009
    Surrey, BC, Canada
    Thanked 2 Times in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Unix/X11 Windows

    Question Use user-specifying QSlider step !

    Hi, all:

    Another naive question.

    I'm using Ubuntu 12.10+Qt Creator 2.5.2 Based on Qt 4.8.2 (32 bit)
    I'm trying to design a slider with interval 2, starting from 1 and ending in 9.
    Namely, I hope the possible values obtained while I'm dragging the slider are:
    1, 3, 5, 7, 9 (all odd numbers)

    And, my code in the produced ui file is copied as follows:
    Qt Code:
    1. horizontalSlider_aperturesize = new QSlider(edgedetectiondialog_laplacian);
    2. horizontalSlider_aperturesize->setObjectName(QString::fromUtf8("horizontalSlider_aperturesize"));
    3. horizontalSlider_aperturesize->setGeometry(QRect(150, 20, 180, 30));
    4. horizontalSlider_aperturesize->setMinimum(1);
    5. horizontalSlider_aperturesize->setMaximum(9);
    6. horizontalSlider_aperturesize->setSingleStep(2);
    7. horizontalSlider_aperturesize->setPageStep(3);
    8. horizontalSlider_aperturesize->setValue(3);
    9. horizontalSlider_aperturesize->setOrientation(Qt::Horizontal);
    10. horizontalSlider_aperturesize->setTickPosition(QSlider::TicksBelow);
    11. horizontalSlider_aperturesize->setTickInterval(2);
    To copy to clipboard, switch view to plain text mode 

    I really can't see anything wrong, but my later-on code went wrong because I got 4 from the slider reading (obviously, this is an even number, which is out of my expectation.)

    Any suggestions on how to avoid such mistakes?

    Thank you very much.

    Best Regards
    Welcome to Vision Open

  2. #2
    Join Date
    Jan 2006
    Graz, Austria
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Unix/X11 Windows

    Default Re: Use user-specifying QSlider step !

    The steps (both single and page) are delta values that are used when a slider is modified by clicking on either side of the handle or by using keyboard keys (cursor keys, page up/down).
    They do not change in any way the sliders ability to be dragged into any position within its range.

    You want a slider that can have five different values, so you create a slider that has a range that only contains five values, e.g. 0 to 4.
    Then you map the slider value to the value you expect at the position, e.g. using a switch statement or, in your example and using range 0-4, using a mathematic function
    Qt Code:
    1. void MyClass::onSliderValueChanged(int value)
    2. {
    3. int actualValue = ( value * 2 ) + 1;
    4. }
    To copy to clipboard, switch view to plain text mode 


Similar Threads

  1. QSlider step customize
    By Kostanev in forum Qt Programming
    Replies: 9
    Last Post: 14th July 2015, 12:47
  2. Replies: 4
    Last Post: 23rd July 2012, 20:09
  3. Qt Creator Debugging step by step through else rich code does not work
    By Terjoqu in forum Qt Tools
    Replies: 1
    Last Post: 11th March 2010, 21:44
  4. learn how to install QT with msvc2005 step by step
    By coder1985 in forum Installation and Deployment
    Replies: 6
    Last Post: 20th December 2007, 19:56
  5. Step by Step MySQL Plugin... what's next step?
    By chaosgeorge in forum Qt Programming
    Replies: 20
    Last Post: 5th November 2006, 15:32

Tags for this Thread


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.