[英]The KeyPressEvent don't work
I have a problem with the KeyPressEvent
, I need to move a QGraphicsItem
player in my scene, but the program does not use the function KeyPressEvent
of class game
我的
KeyPressEvent
有问题,我需要在场景中移动QGraphicsItem
播放器,但是该程序未使用类game
的功能KeyPressEvent
File "game.h": 文件“ game.h”:
#ifndef GAME_H
#define GAME_H
#include <QLayout>
#include <QTimer>
#include <QGraphicsView>
#include <QAction>
#include <QString>
#include <QLabel>
#include <QKeyEvent>
#include <QGraphicsItem>
#include <strutturaAlieni.h>
#include <naveGiocatore.h>
#include <laser.h>
#include <QWidget>
namespace Ui {
class game;
}
class game : public QWidget
{
Q_OBJECT
public:
explicit game(QWidget *parent = 0);
~game();
virtual void keyPressEvent(QKeyEvent *);
}
and the cpp source 和cpp源
void game::keyPressEvent(QKeyEvent *e){
switch(e->key())
{
case Qt::Key_Left :{
this->player->moveBy(-5,0);
break;
}
case Qt::Key_Right :{
this->player->moveBy(5,0);
break;
}
}
this->player->update();
}
and in the mainwindow.cpp I click the play button that executes this code: 在mainwindow.cpp中,单击执行此代码的播放按钮:
void MainWindow::on_Play_clicked()
{
game *Gioco=new game;
Gioco->show();
Gioco->move(300,0);
}
Why doesn't the keyPressEvent
start? 为什么
keyPressEvent
启动?
widget need to have a focus to receive key events. 小部件需要重点关注接收关键事件。 See also QApplication::focusWidget()
另请参见QApplication :: focusWidget()
If you taking about QGraphicsView
then the scene is responsible for focus handling inside of QGraphicsView
. 如果您考虑
QGraphicsView
那么场景将负责QGraphicsView
内部的焦点处理。 See also QGraphicsItem::setFocus 另请参见QGraphicsItem :: setFocus
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.