PDA

View Full Version : Problemas with mouseMoveEvent - Wont Catch Any Movement



21altf
29th October 2011, 19:07
Hey,

I'm having trouble setting up mouseMoveEvent. I setted up a mousePressEvent and it works just fine, but I wont get any response from the
mouseMoveEvent.

I need to catch the mouse position when its moving. Its not implemented yet, but once I grab hold of the mouse movements I will send the positions that are changing to a function to process these movements.

This is the code that I have:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsLineItem>
#include <QGraphicsSceneMouseEvent>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
virtual void mousePressEvent(QMouseEvent *qme1);
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mme2);
void createLine();
qreal x1,x2,y1,y2;
int i;
QGraphicsScene *scene;


~MainWindow();

public slots:
void criarLinha();



private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

-------//------

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsScene>
#include <QGraphicsLineItem>
#include <QEvent>
#include <QDropEvent>
#include <QSignalMapper>
#include <QMouseEvent>
#include <QtDebug>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MainWindow::setCentralWidget(ui->graphicsView);
QObject::connect(ui->action_Line,SIGNAL(triggered()),this,SLOT(criarLin ha()));
ui->graphicsView->setMouseTracking(true);


i = 0;
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;

scene = new QGraphicsScene;
ui->graphicsView->setScene(scene);
scene->installEventFilter(this);



}

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

// Pulbic Functions
void MainWindow::createLine()
{
QGraphicsLineItem *linha = new QGraphicsLineItem(x1,y1,x2,y2);
scene->addItem(linha);
}

void MainWindow::mousePressEvent(QMouseEvent *qme1)
{
QMouseEvent *event = static_cast<QMouseEvent *>(qme1);



if((event->button() == Qt::LeftButton))
{
if(i == 0)
{
x1 = dynamic_cast<QMouseEvent*>(event)->x();
y1 = dynamic_cast<QMouseEvent*>(event)->y();
qDebug() << "x1 é igual a:" << x1;
qDebug() << "y1 é igual a:" << y1;
i = 1;


}
else
{
x2 = dynamic_cast<QMouseEvent*>(event)->x();
y2 = dynamic_cast<QMouseEvent*>(event)->y();
qDebug() << "x2 é igual a:" << x2;
qDebug() << "y2 é igual a:" << y2;
i = 0;
}

}
}

void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEven t *mme)
{

qDebug() << "PQP estou mexendo!!";

}



//Public Slots
void MainWindow::criarLinha()
{
createLine();
}


any help will be appreciated.

tnx,

Added after 54 minutes:

I made it work.

I had to create a class derived from QGraphicView and do another implementation of mouseMoveEvent under it. Any1 can explain why this is necessary?

Why couldnt I just have done it in QMainWindow since it is its central ?

stampede
8th November 2011, 09:47
Why couldnt I just have done it in QMainWindow since it is its central ?
Have you tried to setMouseTracking(true) ?
From the docs:

If mouse tracking is switched off, mouse move events only occur if a mouse button is pressed while the mouse is being moved. If mouse tracking is switched on, mouse move events occur even if no mouse button is pressed.