按键事件不起作用
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();
}
在主窗口.cpp中,我单击执行以下代码的播放按钮:
void MainWindow::on_Play_clicked()
{
game *Gioco=new game;
Gioco->show();
Gioco->move(300,0);
}
为什么keyPressEvent
没有启动?
小部件需要有一个焦点来接收关键事件。另请参阅QApplication::focusWidget()
如果拍摄QGraphicsView
,则场景负责QGraphicsView
内部的焦点处理。另请参阅QGraphicsItem::setFocus
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- SDL2 + Win32 API 菜单栏单击事件不起作用
- Juce 鼠标事件不起作用
- QGraphicsView / Qgraphicsscene Timer事件不起作用
- SFML - 键事件不起作用
- 服务和用户模式进程之间的共享全局事件不起作用
- 大理石鼠标事件在地图qml上不起作用
- Qt C++ QLabel 可点击鼠标事件不起作用
- 使用 EVT_NAVIGATION_KEY 处理事件不起作用
- gtkmm x 事件不起作用
- 按键事件不起作用
- PostMessage(),SendMessage 在 ATL dll 中不起作用(事件处理)
- 使用Qt设计器的Qt按钮事件不起作用:"No such slot QApplication"
- 选择和移动事件在QGraphicsScene中不起作用