I would like to be able to move a QSlider
with arrows of the keyboard at any time.
I want to be able to click anywhere on the QWindow and keep QSlider
activated to move the cursor with the arrows.
My problem is that move the cursor with arrows is only allowed if we click on the QSlider
before.
I hope my question is clear enough.
Does anyone know how to move the QSlider
with arrows of the keyboard without clicking on the QSlider
before please?
There are two approaches:
In Qt terms, you'd like to give slider the focus. Widgets have the setFocus
method, so you need to call slider->setFocus(Qt::OtherFocusReason)
.
Since you want the slider to get focus whenever the underlying window has focus, you need to put the setFocus
call in your implementation of focusInEvent
for the parent widget.
You can forward the key events from the underlying widget to the slider. In the parent widget, reimplement keyPressEvent
and keyReleaseEvent
. When the desired keys are detected, forward them to the slider:
// same for keyReleaseEvent!
void MyWindow::keyPressEvent(QKeyEvent * ev) {
if (ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) {
slider->event(ev);
}
}