如何在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键多少次,都没有反应。我做错了什么?我漏掉了哪一部分?我使用的是Linux Mint和最新版本的Qt。
发布于 2016-03-29 00:15:58
void gameLogic::keybrdStep( QKeyEvent * keypressed )
你从哪里得到方法名"keybrdStep“的?你认为应该给谁打电话?
您需要重写以获取按键操作的方法的名称为QWidget::keyPressEvent()
https://stackoverflow.com/questions/36266200
复制相似问题