PDA

View Full Version : How do I get the value from the slider's position?



JeremyK
18th May 2013, 03:44
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.

Lesiok
18th May 2013, 07:39
QAbstractSlider have method value() and signal valueChanged(int value). Just so look for documentation.

anda_skoa
18th May 2013, 14:54
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,
_

JeremyK
18th May 2013, 16:17
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.


slider->setRange(0, 4);
connect(slider, SIGNAL(valueChanged(int)), SLOT(onSliderValueChanged(int)));

void guiSlider::onSliderValueChanged(int value)
{
switch (value)
{
case 0:
return on_pushButton_clicked();
case 1:
return on_pushButton_1_clicked();
case 2:
return on_pushButton_2_clicked();
case 3:
return on_pushButton_3_clicked();
case 4:
return on_pushButton_4_clicked() ;
}

void guiSlider::on_pushButton_clicked()
{
slider->blockSignals(true);
slider->setValue(0);
slider->blockSignals(false);
}


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!

anda_skoa
18th May 2013, 18:50
Seeing that you only handle values from 0 to 4: have you changed the range of the slider to 0, 4?

Cheers,
_

Lesiok
18th May 2013, 18:51
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?

JeremyK
19th May 2013, 04:38
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

ChrisW67
19th May 2013, 05:51
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.

JeremyK
19th May 2013, 07:19
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.