0

I am making a game requiring qgraphicspixmapitem moving around in the view.

Now, I plan to move my QGraphicsPixmapitem up and down with my keyboard. It works perfectly fine initially.

But since my scene's and view's size are unequal then, which looks pretty wired. So I added some functions to adjust my scene and view's size to be equal.

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

I've tried adding setFocus, Flag to enable my qgraphicspimapitem stay controlled by my keyboard, but in vain.

Any idea will be very appreciated. Thank you!

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

    }
}

main 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();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • thank you soooooo much:) It works magically!!! I've never known eventFilter before, thanks for the precious sharing. I've learned a lot from your answer^^ – bulge teeth Jun 05 '18 at 05:53