PDA

View Full Version : Move Rectangle



Basti1990
28th January 2016, 15:35
hello,

I currently devoloping a point and click adventure as school project.
But I have a problem with the MouseMoveEvent.

I don't know how to implement that, the rectangle moves where I click with the mouse.

I hope someone understands my problem.

Thanks in advance

Best regards

Basti1990

PS code:




// HEADER FILE

#ifndef GAME_H
#define GAME_H

#include <QEvent>
#include <QKeyEvent>
#include <QApplication>
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPainter>
#include <QPointer>
#include <QVariant>
#include <QDebug>
#include <QBrush>
#include <QPen>




class Game : public QWidget
{
Q_OBJECT

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

private:
QGraphicsScene *scene;
QGraphicsView *view;
QGraphicsRectItem *rectangle;

protected:
void mouseMoveEvent(QMouseEvent *mousevent);

};

#endif // GAME_H


// CPP FILE

#include "game.h"

Game::Game(QWidget *parent)
: QWidget(parent)
{
scene = new QGraphicsScene;
view = new QGraphicsView;

view->setScene(scene);

rectangle = new QGraphicsRectItem;
rectangle->setRect(0,0,100,100);
QPen blackpen(Qt::black);
blackpen.setWidth(6);

rectangle->setPen(blackpen);
rectangle->setBrush(QBrush(Qt::white));

rectangle->setFlag(QGraphicsItem::ItemIsFocusable);
rectangle->setFocus();

scene->addItem(rectangle);
scene->setBackgroundBrush(Qt::blue);

// view->setWindowFlags(Qt::FramelessWindowHint);
view->show();
}

Game::~Game()
{}



void Game::mouseMoveEvent(QMouseEvent *mousevent)
{
// SomeThing ?
}


// MAIN FILE

#include "game.h"
#include "menue.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Game w;
w.setVisible(false);

return a.exec();
}



PPS sorry for my terrible english

anda_skoa
28th January 2016, 16:24
It might be better to implement the mouse handling in the graphicsview or in the scene.
In the former case you can map the mouse coordinates of the view manually to the scene, in the latter that has already been taken care of.

Once you have the cooridinates of the click in scene coordinates, you can use rectangle->setPos() to move it to that position

Cheers,
_

Vikram.Saralaya
28th January 2016, 16:30
You have hidden your Game widget.
Your graphicsView does not have a parent, so it is the top level widget.
You are implementing mouseMoveEvent on the Game widget (which is hidden!) so of course you won't receive the event.



First thing:

Replace w.setVisible(false) from your main function with w.show()
Make graphics view the child of Game widget. i.e. replace view = new QGraphicsView; with view = new QGraphicsView(this); If you do this you don't need to show the view manually as you are currently doing.



Once the parent child hierarchy is fixed, you should start getting the mouse events and you can move your rectangle!

Regards
Vikram

Basti1990
28th January 2016, 16:54
Hi,
Thanks for the quick replies,
I'll take care about this.

I'll report me later.

Best regards
Basti1990