Vertical slider again
-
Hello,
My topic is mark as solved but i got a new error :
i have :
Hello guys,
i want to use a vertical slider to increase a variable
void MainWindow::on_verticalSlider_sliderMoved(int position) { ui->verticalSlider->setRange(1,50); z++; }
but as i didn't expected, is that whenever the slider moove he will increment the variable z.
But i want that the slider decrease the variable z when he go down.
and increase when he go up !thanks for your help
-
Hi,
Do you mean you want
z
to have the same value as the slider ? -
Then it's simply:
z = position;
-
void MainWindow::on_verticalSlider_sliderMoved(int position)
{
ui->verticalSlider->setRange(1,50);
z++;
}Well you definitely don't need to call
setRange
more than once on a slider. So remove that line.Then what you need is:
void MainWindow::on_verticalSlider_sliderMoved(int position) { z = position; }
Edit: @SGaist beat me to it. ;)
-
What exactly do you want to do with
z
?What is your goals ?
That would help us understand what you are trying to achieve.
-
Call setRange in your MainWindow constructor.
-
That line:
ui->verticalSlider->setRange(1,50);
you should call it in the MainWindow constructor.And since you are using a Designer based interface you can also set the min and max values in Designer and avoid the setRange call.
-
Well... Yes if you use it somewhere else.
-
Where exactly ? Depending on that, there's not even a need for
z
. You can get the value directly from the slider. -
@Payx said in Vertical slider again:
It depends where i use my "z" ?
How ?
What do you use
z
for? Also side note, that's a horrible name for a variable locally much less one that is used at a class or global level.What @SGaist is saying is you can always get the value of the slider from the slider itself without saving it to a variable. The function
ui->verticalSlider->sliderPosition()
will always contain the same value that yourz
would.So with that in mind you can get rid of that whole overridden function and just call
sliderPosition()
when you need the value.