Re: Keybord shortcuts for hidden widgets / actions
In menubar I have action "Show menu" which is checkable. For that action I have also shortcut and it works great only when menu is visible. When menubar is not visible then shortcut does not work.
How I can implement shortcuts which will work with not visible / hidden actions in menubar?
Thanks,
Added after 41 minutes:
Ok, I found solution - reimplement parent QWidget::keyPressEvent(QKeyEvent *event);
Code:
void MyWidget
::keyPressEvent(QKeyEvent *event
) {
if( event->modifiers() == Qt::ControlModifier ) {
if( event->key() == Qt::Key_M )
// show my menubar
}
but now second question - is it the best solution for that purpose?
Re: Keybord shortcuts for hidden widgets / actions
For a global shortcut I would go for a global event filter, to be sure you catch it independent of which widget has focus.
Cheers,
_
Re: Keybord shortcuts for hidden widgets / actions
anda_skoa you mean something like that:
main.cpp
Code:
int main(int argc, char *argv[])
{
MyWidget w;
app.installEventFilter(&w);
w.show();
return app.exec();
}
event filter in MyWidget
Code:
{
if ( event
->type
() == QEvent::KeyPress) { QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
if( keyEvent->modifiers() == Qt::ControlModifier ) {
switch (keyEvent->key()) {
case Qt::Key_M:
ui->actionShow_menu->setChecked(!ui->actionShow_menu->isChecked());
return true;
case Qt::Key_Left:
prevPage();
return true;
case Qt::Key_Right:
nextPage();
return true;
}
return false;
}
}
return QObject::eventFilter(object, event
);
}
This way is correctly?
Next question, in eventFilter If I want to have "global event handler" I do not need check which object send current event, I am right? So I do not need line:
Code:
if( object == someWidget ) {
// task
}
Thanks,
Re: Keybord shortcuts for hidden widgets / actions
Yes, that looks good, though your switch() should have a default.
And indeed, a global shortcut should work independent of which widget the key would have originally been sent to.
Cheers,
_