1

I have about 5 push buttons and one slider. Every time I click the push button, the function for the particular push button gets called.

However, I also want the slider to do the same. So, instead of pressing the push button, you can drag the slider to the 5 different positions and it will do the same. However, I dont really know how I can connect 5 different positions of the sliders to each push button. Any help would be appreciated.

Thanks

JeremyK
  • 11
  • 2

1 Answers1

2

I don't even know what to say... it's kinda easy:

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

...

void Widget::onSliderValueChanged(int value)
{
    switch (value)
    {
    case 0:
        return onPushButton0Clicked();
    ...
    }
}

void Widget::onPushButton0Clicked()
{
    // do stuff

    slider->blockSignals(true);
    slider->setValue(0);
    slider->blockSignals(false);
}

...
Amartel
  • 4,248
  • 2
  • 15
  • 21
  • Thanks a lot for that! Works now, but the slider does not respond at times while changing it and then suddenly jumps to a value. – JeremyK May 18 '13 at 17:30
  • Your slider now has a range of 5. Which means that moving 1/5 part of it is needed for slider to change value. If you want smooth moving, you could, for example, set range form 0 to 80, and call buttons' slots only for values 0, 20, 40, 60 and 80. You could also add jumping yo nearest valid value on slider's signal `sliderReleased()`. Depending on what you are trying to achieve. – Amartel May 18 '13 at 18:04
  • Thanks for clearing that up. It works now, I had the range all messed up. Thanks again :) – JeremyK May 19 '13 at 06:21