![](/img/trans.png)
[英]Qt5 C++: Subclassing QSpinBox and keyPressEvent won't work
[英]The KeyPressEvent don't work
我的KeyPressEvent
有问题,我需要在场景中移动QGraphicsItem
播放器,但是该程序未使用类game
的功能KeyPressEvent
文件“ 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 *);
}
和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();
}
在mainwindow.cpp中,单击执行此代码的播放按钮:
void MainWindow::on_Play_clicked()
{
game *Gioco=new game;
Gioco->show();
Gioco->move(300,0);
}
为什么keyPressEvent
启动?
小部件需要重点关注接收关键事件。 另请参见QApplication :: focusWidget()
如果您考虑QGraphicsView
那么场景将负责QGraphicsView
内部的焦点处理。 另请参见QGraphicsItem :: setFocus
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.