Results 1 to 9 of 9

Thread: How do I get the value from the slider's position?

  1. #1
    Join Date
    May 2013
    Location
    Amsterdam
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How do I get the value from the slider's position?

    Hi guys,

    I am trying to implement a slider in Qt Eclipse that can respond to changing the slider's position. To give an idea of what I am doing

    I have 5 push buttons and every time you press press the push button, the function for the push button gets called. However, I also want the slider to do what each of the push buttons do. For instance, the first position of the slider does what clicking push button 1 would do, the second position does what click push button 2 would do and so on.

    I tried to connect the slider to the push button using the value changed signal but this only works one time for the first push button.

    Any help is greatly appreciated. Thanks in advance.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I get the value from the slider's position?

    QAbstractSlider have method value() and signal valueChanged(int value). Just so look for documentation.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How do I get the value from the slider's position?

    Quote Originally Posted by JeremyK View Post
    I tried to connect the slider to the push button using the value changed signal but this only works one time for the first push button.
    Instead of that you connect the valueChanged(int) signal to a slot and the do a switch on the value passed to that slot.
    For each value the respective case statement will then call the appropriate slot.

    Cheers,
    _

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

    JeremyK (19th May 2013)

  5. #4
    Join Date
    May 2013
    Location
    Amsterdam
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do I get the value from the slider's position?

    Quote Originally Posted by anda_skoa View Post
    Instead of that you connect the valueChanged(int) signal to a slot and the do a switch on the value passed to that slot.
    For each value the respective case statement will then call the appropriate slot.

    Cheers,
    _
    It makes some sense now. Thanks. Got this done with some help.

    Qt Code:
    1. slider->setRange(0, 4);
    2. connect(slider, SIGNAL(valueChanged(int)), SLOT(onSliderValueChanged(int)));
    3.  
    4. void guiSlider::onSliderValueChanged(int value)
    5. {
    6. switch (value)
    7. {
    8. case 0:
    9. return on_pushButton_clicked();
    10. case 1:
    11. return on_pushButton_1_clicked();
    12. case 2:
    13. return on_pushButton_2_clicked();
    14. case 3:
    15. return on_pushButton_3_clicked();
    16. case 4:
    17. return on_pushButton_4_clicked() ;
    18. }
    19.  
    20. void guiSlider::on_pushButton_clicked()
    21. {
    22. slider->blockSignals(true);
    23. slider->setValue(0);
    24. slider->blockSignals(false);
    25. }
    To copy to clipboard, switch view to plain text mode 

    Thats Part of the code. It works now, however the slider seems to respond at times and does not respond at times. For instance, if I move it to say 10, it works but then stays there regardless of moving the sllider. All of a sudden, it then jumps to 30 or 40. Any idea why?

    Thanks again!
    Last edited by JeremyK; 18th May 2013 at 18:27.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How do I get the value from the slider's position?

    Seeing that you only handle values from 0 to 4: have you changed the range of the slider to 0, 4?

    Cheers,
    _

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

    JeremyK (19th May 2013)

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I get the value from the slider's position?

    JeremyK, for me this is some nonsense what You are doing - when new value of slider is 0 You are once more set it to 0. I suspect that the other methods are identical. What you really want to do?

  9. #7
    Join Date
    May 2013
    Location
    Amsterdam
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do I get the value from the slider's position?

    Quote Originally Posted by Lesiok View Post
    JeremyK, for me this is some nonsense what You are doing - when new value of slider is 0 You are once more set it to 0. I suspect that the other methods are identical. What you really want to do?
    I just want the slider carry out the same function as each of the push buttons do. But instead of pressing each push button individually, I would rather that moving the slider would have the same effect.

    Cheers

  10. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How do I get the value from the slider's position?

    For instance, if I move it to say 10, it works but then stays there regardless of moving the sllider. All of a sudden, it then jumps to 30 or 40. Any idea why?
    You have a slider with a range of zero to four, but are complaining about something with a value of 10, 30 or 40?

    The jerky behaviour is probably because whatever is executing in the slots associated with 1, 2, 3 etc. is taking some time to execute and blocking the UI while it does so. The slider emits a valueChanged() for every value you pass through, not just the value where you release the slider button.

    Ultimately though I have no idea what you are trying to achieve. The whole idea of using the position of a slider to execute discrete functions more logically assigned to buttons is truly odd. If you want all five actions to occur in sequence then just call the slot for each button in turn in response to pushing a "Do the lot" button.

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

    JeremyK (19th May 2013)

  12. #9
    Join Date
    May 2013
    Location
    Amsterdam
    Posts
    4
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do I get the value from the slider's position?

    Quote Originally Posted by ChrisW67 View Post
    You have a slider with a range of zero to four, but are complaining about something with a value of 10, 30 or 40?

    The jerky behaviour is probably because whatever is executing in the slots associated with 1, 2, 3 etc. is taking some time to execute and blocking the UI while it does so. The slider emits a valueChanged() for every value you pass through, not just the value where you release the slider button.

    Ultimately though I have no idea what you are trying to achieve. The whole idea of using the position of a slider to execute discrete functions more logically assigned to buttons is truly odd. If you want all five actions to occur in sequence then just call the slot for each button in turn in response to pushing a "Do the lot" button.
    It makes sense now. Got everything sorted.
    Thanks a lot for the help guys.

Similar Threads

  1. view position of mouse position in GraphicsScene
    By Raghaw in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2012, 04:46
  2. Replies: 4
    Last Post: 3rd August 2010, 12:30
  3. help on my slider
    By newb in forum Qt Programming
    Replies: 1
    Last Post: 13th July 2010, 15:36
  4. Replies: 2
    Last Post: 21st March 2010, 09:01
  5. get slider value
    By eric in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2007, 19:47

Tags for this Thread

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.