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

时间:2018-06-04 08:13:38

标签: c++ qt qgraphicsview qgraphicsscene qmainwindow

我正在制作一款游戏,要求qgraphicspixmapitem在视图中移动。

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

但是因为我的场景和视图的大小不相等,看起来非常有线。所以我添加了一些函数来调整我的场景和视图的大小相等。

  

然而,在设置我的QGraphicsScene和QGraphicsView相同的大小后,   我再也不能用我的键盘移动我的qgraphicspixmapitem了。

我尝试添加 setFocus,Flag 以使我的qgraphicspimapitem保持由我的键盘控制,但是徒劳无功。

任何想法都将非常感激。谢谢!

主窗口.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);
    }

    }
}

主窗口

#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();
}

1 个答案:

答案 0 :(得分:0)

You have never been able to move the item, what you have done is to move the scrollbars making the same effect in the visual part.

What you have to do is use the KeyPress event of the QGraphicsView, one way to do it is to create a new class that inherits from QGraphicsView, but another simpler way is to install an event filter as shown below:

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;
}

In the following link you can find the complete example