【发布时间】:2014-03-07 19:41:06
【问题描述】:
用户可以通过“Tab”键或箭头键“”来逐步浏览 QtGUI 的小部件。
有人知道如何为此禁用箭头键吗?我需要箭头键来做其他事情。
【问题讨论】:
标签: c++ qt qwidget qtgui qevent
用户可以通过“Tab”键或箭头键“”来逐步浏览 QtGUI 的小部件。
有人知道如何为此禁用箭头键吗?我需要箭头键来做其他事情。
【问题讨论】:
标签: c++ qt qwidget qtgui qevent
您需要在自己的 QWidget 子类中重新实现 corresponding event,如下所示:
bool MyWidget::keyPressEvent(QKeyEvent *keyEvent)
{
if (keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right) {
// Do nothing
} else {
QWidget::keyPressEvent(keyEvent);
}
}
【讨论】:
只需重新实现主窗口的 event() 或 keyPressEvent() / keyReleaseEvent()。在重新实现的方法中,您可以放置所需的操作。
【讨论】:
我可能会为此目的使用 QAction。所以你不需要子类化。
QTabBar *tabBar;
........................
QAction* pLeftArrowAction = new QAction(this);
pLeftArrowAction->setShortcut(Qt::Key_Left);
QAction* pRightArrowAction = new QAction(this);
pRightArrowAction->setShortcut(Qt::Key_Right);
tabBar->addActions(QList<QAction*>() << pLeftArrowAction << pRightArrowAction);
【讨论】: