Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use user-specifying QSlider step

Use user-specifying QSlider step

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jiapei100J Offline
    jiapei100J Offline
    jiapei100
    wrote on last edited by
    #1

    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:
    @horizontalSlider_aperturesize = new QSlider(edgedetectiondialog_laplacian);
    horizontalSlider_aperturesize->setObjectName(QString::fromUtf8("horizontalSlider_aperturesize"));
    horizontalSlider_aperturesize->setGeometry(QRect(150, 20, 180, 30));
    horizontalSlider_aperturesize->setMinimum(1);
    horizontalSlider_aperturesize->setMaximum(9);
    horizontalSlider_aperturesize->setSingleStep(2);
    horizontalSlider_aperturesize->setPageStep(3);
    horizontalSlider_aperturesize->setValue(3);
    horizontalSlider_aperturesize->setOrientation(Qt::Horizontal);
    horizontalSlider_aperturesize->setTickPosition(QSlider::TicksBelow);
    horizontalSlider_aperturesize->setTickInterval(2);
    @

    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
    Pei

    Welcome to Longer Vision
    https://www.longervision.com

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      I think you should create your Slider class inherithed from QSlider and reimplement setValue() slot to force values only to odd ones.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TheArchCoder
        wrote on last edited by
        #3

        Here's some c++ code to fix this, I used this method when i was having the same problem.
        Note that i am not experienced with c++, So i apologize for any mistakes in the code below.

        Connect this function to slider.valueChanged:

        void slider_value_mapper(int v) {
            int steps = 10; // Enter the amount of user specified steps you want here.
            slider->setValue(static_cast<int>(v / steps) * steps); // Static cast to make sure its not a floating point number.
        };
        

        Another quick note, The slider may not be able to go to the end. You may wanna adjust its range for that, If it happens.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved