【发布时间】:2016-03-28 16:11:13
【问题描述】:
如何在 Qt 中实现键盘监听?我的以下设置不起作用。我有两个类,gameLogic 和 gameView。 gameView 有一个 gameLogic 实例:
gameView::gameView(QWidget *parent)
: QWidget(parent)
{
logic = new gameLogic(6);
logic->setFocusPolicy(Qt::TabFocus); //in one of the articles I read, this was supposed to fix the issue. It doesn't for me.
this->resize(1200, 700);
this->setStyleSheet("background-color: white");
QString str;
str.setNum(logic->n);
connect(logic, SIGNAL(playerStepped(int, int)), this, SLOT(movePlayer(int, int)));
}
在 gameLogic 中,我按如下方式处理击键:
void gameLogic::keybrdStep( QKeyEvent * keypressed )
{
if (keypressed->key() == Qt::Key_Q) {
_message = new QMessageBox;
_message->setText("Q");
_message->exec();
}
}
无论我按 Q 多少次,都没有任何反应。我究竟做错了什么?我错过了哪一部分?我正在使用最新版本的 Qt 使用 Linux Mint。
【问题讨论】:
标签: c++ qt keyboard-events