Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.
-
Hi there,
in a bigger project I try too create a dialog for editing whitepoint and greybalance.
In order to trace and detect my issue I have extracted the minimal code.What do I have: A dialog with 2 GroupBoxes. Each has a GraphicsView and his own scene.
After painting a background I add an ellipse-item and set:QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem (250 -5, 250 - 5, 10, 10); ellipse->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemSendsScenePositionChanges | QGraphicsItem::ItemSendsGeometryChanges); scene->addItem(ellipse);I do this, because I aim to grab and move this circle - which works.
Now I try to react on the movement and position changes. In my minimal.h .void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);In my minimal.cpp
void WpEditor::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { mPoint = event->pos(); qDebug() << "Mouse move " << mPoint; QGraphicsScene::mouseMoveEvent(event); }My issue: Nothing happens. What I am doing wrong here.
thx and regards
PS: If you need the full example pls let me know where to send it. I still can't upload here.
-
Hi there,
in a bigger project I try too create a dialog for editing whitepoint and greybalance.
In order to trace and detect my issue I have extracted the minimal code.What do I have: A dialog with 2 GroupBoxes. Each has a GraphicsView and his own scene.
After painting a background I add an ellipse-item and set:QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem (250 -5, 250 - 5, 10, 10); ellipse->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemSendsScenePositionChanges | QGraphicsItem::ItemSendsGeometryChanges); scene->addItem(ellipse);I do this, because I aim to grab and move this circle - which works.
Now I try to react on the movement and position changes. In my minimal.h .void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);In my minimal.cpp
void WpEditor::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { mPoint = event->pos(); qDebug() << "Mouse move " << mPoint; QGraphicsScene::mouseMoveEvent(event); }My issue: Nothing happens. What I am doing wrong here.
thx and regards
PS: If you need the full example pls let me know where to send it. I still can't upload here.
@ademmler
You mean your method is not hit? Start by appendingoverridein the.hfile to be sure.WpEditoris indeed aQGraphicsSceneand not aQGraphicsView, right?In the event I think you will want to use
event->scenePos();rather thanevent->pos();. -
It might be easier to sub-class QGraphicsObject and then make connections to its x/yChanged() signals.
-
@ademmler
You mean your method is not hit? Start by appendingoverridein the.hfile to be sure.WpEditoris indeed aQGraphicsSceneand not aQGraphicsView, right?In the event I think you will want to use
event->scenePos();rather thanevent->pos();.@JonB said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:
Dear JonB - thx for your feedback.
You mean your method is not hit? Start by appending
overridein the.hfile to be sure.When I do so I get:
error: non-virtual member function marked 'override' hides virtual member function
Honestly I have never understood when I can override a QT function and when I can't.WpEditor
is indeed aQGraphicsSceneand not aQGraphicsView`, right?It is a QGrahicsview (in an UI file) which I attache a scene to. In the constructor I do:
... some stuff
sceneWp = new QGraphicsScene(0, 0, 500, 500);
viewWp->setScene(sceneWp);
... some more stuffIn the event I think you will want to use
event->scenePos();rather thanevent->pos();.Yes this is correct - my fault !
-
@JonB said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:
Dear JonB - thx for your feedback.
You mean your method is not hit? Start by appending
overridein the.hfile to be sure.When I do so I get:
error: non-virtual member function marked 'override' hides virtual member function
Honestly I have never understood when I can override a QT function and when I can't.WpEditor
is indeed aQGraphicsSceneand not aQGraphicsView`, right?It is a QGrahicsview (in an UI file) which I attache a scene to. In the constructor I do:
... some stuff
sceneWp = new QGraphicsScene(0, 0, 500, 500);
viewWp->setScene(sceneWp);
... some more stuffIn the event I think you will want to use
event->scenePos();rather thanevent->pos();.Yes this is correct - my fault !
@ademmler said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:
error: non-virtual member function marked 'override' hides virtual member function
Show the code. You're not correctly overriding.
-
@JonB said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:
Dear JonB - thx for your feedback.
You mean your method is not hit? Start by appending
overridein the.hfile to be sure.When I do so I get:
error: non-virtual member function marked 'override' hides virtual member function
Honestly I have never understood when I can override a QT function and when I can't.WpEditor
is indeed aQGraphicsSceneand not aQGraphicsView`, right?It is a QGrahicsview (in an UI file) which I attache a scene to. In the constructor I do:
... some stuff
sceneWp = new QGraphicsScene(0, 0, 500, 500);
viewWp->setScene(sceneWp);
... some more stuffIn the event I think you will want to use
event->scenePos();rather thanevent->pos();.Yes this is correct - my fault !
@ademmler said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:
error: non-virtual member function marked 'override' hides virtual member function
Honestly I have never understood when I can override a QT function and when I can't.This is totally a C++ issue, nothing special about Qt. And it is really easy to tell:
- If a method is marked
virtualyou canoverrideit. And Qt docs will always showvirtualfor such methods. - If a method is not marked
virtualyou cannotoverrideit.
"Simplez" :)
overrideis not mandatory, but I suggest you should always put it in if you are intending to override a method. Because you get an error then if the method, or its signature, are not virtual/overridable, and you will know. And in this case the error message tells you exactly that.So you must either sub-class your
QGraphicsSceneand override itsvoid mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)(which I have never tried) or you need to override theQGraphicsView::mouseMoveEvent(QMouseEvent *event)in yourWpEditor. In this case, confirm that you will keep the left mouse pressed down while you do drag-move, else you will need to callQGraphicsView::setMouseTracking(true).Meanwhile, I noticed your original
QGraphicsScene::mouseMoveEvent(event);inWpEditor::mouseMoveEvent(), your attempt to call the base class implementation. I took you at your word, but you needQGraphicsView::mouseMoveEvent(event);instead! - If a method is marked
-
@ademmler said in Cant get "mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)" to work.:
error: non-virtual member function marked 'override' hides virtual member function
Honestly I have never understood when I can override a QT function and when I can't.This is totally a C++ issue, nothing special about Qt. And it is really easy to tell:
- If a method is marked
virtualyou canoverrideit. And Qt docs will always showvirtualfor such methods. - If a method is not marked
virtualyou cannotoverrideit.
"Simplez" :)
overrideis not mandatory, but I suggest you should always put it in if you are intending to override a method. Because you get an error then if the method, or its signature, are not virtual/overridable, and you will know. And in this case the error message tells you exactly that.So you must either sub-class your
QGraphicsSceneand override itsvoid mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)(which I have never tried) or you need to override theQGraphicsView::mouseMoveEvent(QMouseEvent *event)in yourWpEditor. In this case, confirm that you will keep the left mouse pressed down while you do drag-move, else you will need to callQGraphicsView::setMouseTracking(true).Meanwhile, I noticed your original
QGraphicsScene::mouseMoveEvent(event);inWpEditor::mouseMoveEvent(), your attempt to call the base class implementation. I took you at your word, but you needQGraphicsView::mouseMoveEvent(event);instead!@JonB thx again JonB.
Either I subclass QGraphicsScene - as you wrote before or using an event filter might help. I found this thread about it.
https://stackoverflow.com/questions/51151806/use-a-qgraphicsscenemouseevent-in-a-class-that-inherits-from-qgraphicsviewbool DisplayImage::eventFilter(QObject *watched, QEvent *event) { if(watched == scene){ // press event QGraphicsSceneMouseEvent *mouseSceneEvent; if(event->type() == QEvent::GraphicsSceneMousePress){ mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event); qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos(); // your logic here } // move event else if (event->type() == QEvent::GraphicsSceneMouseMove) { mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event); qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos(); // your logic here } // release event else if (event->type() == QEvent::GraphicsSceneMouseRelease) { mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event); qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos(); // your logic here } } return QGraphicsView::eventFilter(watched, event); } - If a method is marked
-
@JonB thx again JonB.
Either I subclass QGraphicsScene - as you wrote before or using an event filter might help. I found this thread about it.
https://stackoverflow.com/questions/51151806/use-a-qgraphicsscenemouseevent-in-a-class-that-inherits-from-qgraphicsviewbool DisplayImage::eventFilter(QObject *watched, QEvent *event) { if(watched == scene){ // press event QGraphicsSceneMouseEvent *mouseSceneEvent; if(event->type() == QEvent::GraphicsSceneMousePress){ mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event); qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos(); // your logic here } // move event else if (event->type() == QEvent::GraphicsSceneMouseMove) { mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event); qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos(); // your logic here } // release event else if (event->type() == QEvent::GraphicsSceneMouseRelease) { mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event); qDebug()<<mouseSceneEvent->scenePos()<<mouseSceneEvent->lastScenePos(); // your logic here } } return QGraphicsView::eventFilter(watched, event); } -
What are you actually trying to do? I had suggested using a QGraphicsObject and its signals because I thought you were moving around the ellipse you create to change two parameters that affect other things in your application/UI and its position would reflect the current value of those parameters. If this is the case, even if you do this by watching mouse events in the scene, you will want to use changes in the ellipse object's position to make your parameter changes.
-
What are you actually trying to do? I had suggested using a QGraphicsObject and its signals because I thought you were moving around the ellipse you create to change two parameters that affect other things in your application/UI and its position would reflect the current value of those parameters. If this is the case, even if you do this by watching mouse events in the scene, you will want to use changes in the ellipse object's position to make your parameter changes.