QT:即使在 setfocus() 之后也无法用键盘控制我的 QGraphicsPixmapItem

QT: cannot control my QGraphicsPixmapItem with keyboard even after setfocus()

我正在制作一个需要 qgraphicspixmapitem 在视图中移动的游戏。

现在,我打算用键盘上下移动我的 QGraphicsPixmapitem。它最初工作得很好。

但是由于我的场景和视图的大小不相等,所以看起来很连线。所以我添加了一些功能来调整我的场景和视图的大小相等。

However, after setting my QGraphicsScene and QGraphicsView same size, I cannot move my qgraphicspixmapitem with my keyboard anymore.

我试过添加 setFocus, Flag 来让我的 qgraphicspimapitem 保持由我的键盘控制,但是没有用。

任何想法将不胜感激。谢谢!

主要window.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{ 
    //scene
    scene = new QGraphicsScene(0, 0, 1050, 600);

    //player
    player = new QGraphicsPixmapItem(QPixmap(":/img/whitedog.png").scaled(100,100));
    player->setFlag(QGraphicsPixmapItem:: ItemIsFocusable,true);
    player->setFocus();
    player->setPos(350, 500);
    scene->addItem(player);
    playertimer->start(10);

    //view
    view = new QGraphicsView(this);
    view ->setScene(scene);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setCentralWidget(view);
    view->setFixedSize(1055,605);
}

void MainWindow::keyPressEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Down){
        player->setPos(player->x(),player->y()+10);
    }

    else if (e->key() == Qt::Key_Up){
        player->setPos(player->x(),player->y()-10);
    }

    }
}

主要window.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QKeyEvent>
#include <QtGui>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    virtual void keyPressEvent(QKeyEvent *e);

private:
    Ui::MainWindow *ui;
    QGraphicsScene *scene;
    QGraphicsItem *player;
    QGraphicsView * view;
};

#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

您一直无法移动项目,您所做的是在视觉部分移动滚动条以达到相同的效果。

您需要做的是使用 QGraphicsViewKeyPress 事件,一种方法是创建一个继承自 QGraphicsView 的新 class ,但另一种更简单的方法是安装事件过滤器,如下所示:

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QKeyEvent>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //scene
    scene = new QGraphicsScene(0, 0, 1050, 600);
    //player
    player = new QGraphicsPixmapItem(QPixmap(":/img/whitedog.png").scaled(100,100));
    player->setPos(350, 500);
    scene->addItem(player);
    //view
    view = new QGraphicsView;
    view ->setScene(scene);
    view->installEventFilter(this);
    setCentralWidget(view);
    view->setFixedSize(1055,605);
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == view && event->type() == QEvent::KeyPress){
        QKeyEvent *kevent = static_cast<QKeyEvent *>(event);
        if(kevent->key() == Qt::Key_Down){
            player->setPos(player->pos() + QPointF(0, 10));
            return true;
        }
        else if(kevent->key() == Qt::Key_Up){
            player->setPos(player->pos() - QPointF(0, 10));
            return true;
        }
    }
    return QMainWindow::eventFilter(watched, event);
}

MainWindow::~MainWindow()
{
    delete ui;
}

在下面link你可以找到完整的例子