Re: Problemas with mouseMoveEvent - Wont Catch Any Movement
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:
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsLineItem>
#include <QGraphicsSceneMouseEvent>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
void createLine();
qreal x1,x2,y1,y2;
int i;
~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
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
MainWindow::setCentralWidget(ui->graphicsView);
QObject::connect(ui
->action_Line,
SIGNAL(triggered
()),
this,
SLOT(criarLinha
()));
ui->graphicsView->setMouseTracking(true);
i = 0;
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;
ui->graphicsView->setScene(scene);
scene->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
// Pulbic Functions
void MainWindow::createLine()
{
scene->addItem(linha);
}
{
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;
}
}
}
{
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 ?
Re: Problemas with mouseMoveEvent - Wont Catch Any Movement
Quote:
Why couldnt I just have done it in QMainWindow since it is its central ?
Have you tried to setMouseTracking(true) ?
From the docs:
Quote:
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.