QGraphicsView mouse move event not work to me
-
I have a qgraphicsview to plot signal. I would zoom specific area with mouse clicking and rectangle drawing. So I need mouse pressed position and dragged position. For this I do such this:
in header file:
class QtGuiApplication : public QMainWindow { Q_OBJECT public: QtGuiApplication(QWidget *parent = Q_NULLPTR); protected: void mouseMoveEvent(QMouseEvent* event); void mousePressEvent(QMouseEvent* event); bool eventFilter(QObject *obj, QEvent *ev); private: QPoint Zoom_point1_; QPoint Zoom_point2_; QGraphicsScene* scene = new QGraphicsScene(); };
in source file:
QtGuiApplication::QtGuiApplication(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); ui.graphicsView->installEventFilter(this); ui.graphicsView->setMouseTracking(true); } bool QtGuiApplication::eventFilter(QObject * obj, QEvent * ev) { if (obj == ui.graphicsView) if (ev->type() == QEvent::MouseMove) { QMouseEvent *mEvent = (QMouseEvent*)ev; Zoom_point2_ = mEvent->pos(); } return false; } void QtGuiApplication::mouseMoveEvent(QMouseEvent * ev) { Zoom_point2_ = ev->globalPos(); //do some thing … } void QtGuiApplication::mousePressEvent(QMouseEvent * ev) { Zoom_point1_ = ev->globalPos(); }
When I press and move mouse in graphicsview, I can recognize the clicked position but
mouseMoveEvent(QMouseEvent * ev)
never be called! and alsoobj == ui.graphicsView
statement ineventFilter
never be occurred.What's wrong with me? How can I fix it? -
Hi,
Why are you re-implementing QMainWindow mouse related events as well as filter QGraphicsView ?
-
Hi,
I think you have your event filter installation backward:
ui.graphicsView->installEventFilter(this);
Should be:
this->installEventFilter(ui->graphicsView);
So here, you are saying your mainWindow should spy on the event destined to your ui.graphicsView, while it's actually the opposite that you want.