QSlider linked with other QSlider - hints needed
-
Hello colleagues
I'm working on the such topic:
- I have 2 objects of MirroredSlider : public QSlider
- Both have the same singleStep and pageStep set.
- The goal I'd like to achieve is - when I hold down CTRL key, and change value of the 1st slider using GUI input then the other slider accordingly goes up/down single step. As a bonus - would be nice to handle usual keyboard input for QSlider too.
- Starting values of both sliders don't have to match.
Tried reimplementing events, like
void MirroredSlider::wheelEvent(QWheelEvent *event) { if (mirroredSlider && event->modifiers() == Qt::ControlModifier) { QWheelEvent *e = new QWheelEvent(QPointF(), 0, 0, 0); *e = *event; e->setModifiers(Qt::NoModifier); qApp->postEvent(mirroredSlider, e); } QSlider::wheelEvent(event); } //and for grabbing and moving slider handle void MirroredSlider::mouseMoveEvent(QMouseEvent *event) { oldValue = value(); QSlider::mouseMoveEvent(event); if (mirroredSlider && event->modifiers() == Qt::ControlModifier) { if (oldValue > value()) { mirroredSlider->triggerAction(QAbstractSlider::SliderSingleStepSub); } else if (oldValue < value()) { mirroredSlider->triggerAction(QAbstractSlider::SliderSingleStepAdd); } } }
The above works fine for mouse wheel and moving handle by mouse, but fails when I point anywhere on the slider, and press and hold left mouse button; btw a curiosity, log placed inside event(QEvent*) for that case shows 1st mousePress, then timer events as longs as the button is held down, then mouseRelease.
I tried to use signal valueChanged() and actionTriggered(), but also turned out to be not good enough solution.
Is there something else I should put my attention to?
-
-
@MasterBLB said in QSlider linked with other QSlider - hints needed:
And again, soon after I post here I get stroke of genius, and find solution on my own. Happens almost every time.
Rubber Duck Debugging at its best :))
-
@Pl45m4 said in QSlider linked with other QSlider - hints needed:
@MasterBLB said in QSlider linked with other QSlider - hints needed:
And again, soon after I post here I get stroke of genius, and find solution on my own. Happens almost every time.
Rubber Duck Debugging at its best :))
Yepp, though before posting I spent several hours testing different approaches - most interesting finding was that for press and hold lmb on non-handle element of the slider invokes timer events. The most important discovery was:
- value() returns something
- an event happens
- if the event was some kind which affect the value now, just after QSlider::event(), the value() should be different
after realizing that the rest was easy.