Problem in getting focus?
-
In my program i am reimplementing key press event but it is not getting called in pressing the arrow key
@void slider::keyPressEvent (QKeyEvent *event)
{
qDebug ()<<"keypress";
if (slider_mode) {
if (event->key () == Qt::Key_Left) {
event->accept ();thumb_pos_groove = thumb_pos_groove - 1; calcNewValue (); emit sliderMoved (value_pos_hori); this->repaint (); } else if (event->key () == Qt::Key_Right){ event->accept (); thumb_pos_groove = thumb_pos_groove + 1; calcNewValue (); emit sliderMoved (value_pos_hori); this->repaint (); } } else { if (event->key () == Qt::Key_Up) { event->accept (); thumb_pos_groove = thumb_pos_groove - 1; calcNewValue (); //emit sliderMoved (value_pos_verti); this->repaint (); } else if (event->key () == Qt::Key_Down){ event->accept (); thumb_pos_groove = thumb_pos_groove + 1; calcNewValue (); //emit sliderMoved (value_pos_verti); this->repaint (); } }
}@
. I am not able to find what is the problem . -
This could be tricky, but could it be the slider_mode boolean not set?
-
no because i am always setting slider mode .
-
[quote author="pratik041" date="1321346642"]no because i am always setting slider mode .[/quote]
Apologize me for insisting, but setting slider_mode will result in avoiding the execution of the key.up/down handling. So if you are seeing the debug message but nothing happens when you push up/down then it is the boolean flag interfering with your code. Otherwise does the code for the up key work? Could you check exactly what key event is generated when you push the down key?
-
i am not seeing debug message itself.
-
Things I would check:
is there an event filter installed somewhere?
is the widget getting the focus properly?
-
no event filter is installed
I am making a custom slider and using arrow key for that only. I think the slider is not getting focus because while pressing up and down arrow key after clicking slider the spin box value is changing not slider. -
If I get it right, the problem seems to be that the spinbox has the focus and not the slider. If that is the case, my first thoughts will be to either
- give back the focus to the slider after any change in the spinbox
- propagate the keypress event from the spin box to the slider (so implement a keyevent listener in the spinbox too and send the event to the slider).
But in both cases, please note that any widget in the window will have to do the same, or your changes will be vanished from adding new widgets. So, if this is the problem, I suggest to implement the event listener in the main widget and propagate it to the slider.
Someone could have better suggestions.