How to remove spacebar action from QToolButton
I have a QToolBar on which there is a QToolButton. When QToolButton is pressed by mouse click then it perform some action. The same action is performed when space bar is pressed. I dont want the action to be fire开发者_StackOverflow中文版d on space bar press, but I want it on mouse click. How I can do this?
Subclass QToolButton using inheritance and override QWidget::keyPressEvent()
. There, check if the key you get is Qt::Key_Space
and if it is, return and do nothing. if it isn't pass the event to QToolButton.
Use the set focus policy with no focus, this prevents the keyboard from having focus but still allows mouse clicks.
Button->setFocusPolicy(Qt::NoFocus)
Make sure you didn't accidentally assign 'Space' as the shortcut to the action associated with the QToolButton in question (check out Qt Creater's action editor).
精彩评论