Qt标签顺序键

时间:2014-03-07 19:41:07

标签: c++ qt qwidget qtgui qevent

用户可以通过键“Tab”或通过箭头键“< - ”和“ - >”逐步浏览QtGUI的小部件。

有人知道如何为此目的禁用箭头键吗?我需要箭头键来做别的事情。

3 个答案:

答案 0 :(得分:3)

您需要重新实现自己的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);
    }
}

答案 1 :(得分:1)

只需重新实现主窗口的event()或keyPressEvent()/ keyReleaseEvent()。在重新实现的方法中,您可以放置​​所需的操作。

答案 2 :(得分:1)

我可以将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);