QGraphicsSceneMouseEvent not working
-
Hi everybody,
I'm trying to move an object in QGraphicsScene with the mouse.
Cannot find out why it's not working.
Here's what I'm doing:Simple GraphicScene with some Items in a view:
setGeometry(1400, 500, 500, 500); scene = new QGraphicsScene(0, 0, 400, 400); view = new QGraphicsView(scene, this); view->setGeometry(QRect(5, 30, 400, 400)); view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); QPen receiverPen(qRgb(80, 255, 47)); QPen actorPen(Qt::red); QPen linePen(Qt::black); linePen.setWidth(2); QBrush receiverBrush(qRgb(80, 255, 47)); QBrush actorBrush(Qt::red); receiver1 = scene->addEllipse(-10, -10, 20, 20, receiverPen, receiverBrush); receiver2 = scene->addEllipse(scene->width() - 10, -10, 20, 20, receiverPen, receiverBrush); actor = scene->addEllipse(scene->width()/2 - 5, scene->height()/2 - 5, 10, 10, actorPen, actorBrush);
And then the method that shoud enable me to move the actor-Item:
void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { actor->setPos(event->pos()); statusBar()->showMessage(QString::number(event->pos().x())); }
I added the statusbarmessage to see, if the event is arriving at all. It seems not to.
When I try with a QMouseEvent it does, but only outside the QGraphicsView...Would be very happy, if somebody could tell me what I'm missing...
Thanks, Michael
-
@herrgross said in QGraphicsSceneMouseEvent not working:
But I had no access to the position data, that's why I tried with mouseMoveEvent. Now I am working around with the elasticnodes-example, that @JoeCFD recommanded, that seems to work, but is a lot more work...
If you subclass either the item or the scene, you can react on position changes or read the new item position.
Using the base classes only, things are tough. It's weird anyway, since as soon as you want custom behavior, it would end there without any re-implementations or subclassing. -
check the example out
https://doc.qt.io/qt-6/qtwidgets-graphicsview-elasticnodes-example.html -
Hi everybody,
I'm trying to move an object in QGraphicsScene with the mouse.
Cannot find out why it's not working.
Here's what I'm doing:Simple GraphicScene with some Items in a view:
setGeometry(1400, 500, 500, 500); scene = new QGraphicsScene(0, 0, 400, 400); view = new QGraphicsView(scene, this); view->setGeometry(QRect(5, 30, 400, 400)); view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); QPen receiverPen(qRgb(80, 255, 47)); QPen actorPen(Qt::red); QPen linePen(Qt::black); linePen.setWidth(2); QBrush receiverBrush(qRgb(80, 255, 47)); QBrush actorBrush(Qt::red); receiver1 = scene->addEllipse(-10, -10, 20, 20, receiverPen, receiverBrush); receiver2 = scene->addEllipse(scene->width() - 10, -10, 20, 20, receiverPen, receiverBrush); actor = scene->addEllipse(scene->width()/2 - 5, scene->height()/2 - 5, 10, 10, actorPen, actorBrush);
And then the method that shoud enable me to move the actor-Item:
void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { actor->setPos(event->pos()); statusBar()->showMessage(QString::number(event->pos().x())); }
I added the statusbarmessage to see, if the event is arriving at all. It seems not to.
When I try with a QMouseEvent it does, but only outside the QGraphicsView...Would be very happy, if somebody could tell me what I'm missing...
Thanks, Michael
@herrgross said in QGraphicsSceneMouseEvent not working:
void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
This is already not correct.
MainWindow
does not receiveQGraphicsSceneMouseEvent
s.I'm trying to move an object in QGraphicsScene with the mouse.
actor->setFlag(QGraphicsItem::ItemIsMovable);
Done.
-
@herrgross said in QGraphicsSceneMouseEvent not working:
void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
This is already not correct.
MainWindow
does not receiveQGraphicsSceneMouseEvent
s.I'm trying to move an object in QGraphicsScene with the mouse.
actor->setFlag(QGraphicsItem::ItemIsMovable);
Done.
-
@herrgross said in QGraphicsSceneMouseEvent not working:
But I had no access to the position data, that's why I tried with mouseMoveEvent. Now I am working around with the elasticnodes-example, that @JoeCFD recommanded, that seems to work, but is a lot more work...
If you subclass either the item or the scene, you can react on position changes or read the new item position.
Using the base classes only, things are tough. It's weird anyway, since as soon as you want custom behavior, it would end there without any re-implementations or subclassing. -
@herrgross said in QGraphicsSceneMouseEvent not working:
But I had no access to the position data, that's why I tried with mouseMoveEvent. Now I am working around with the elasticnodes-example, that @JoeCFD recommanded, that seems to work, but is a lot more work...
If you subclass either the item or the scene, you can react on position changes or read the new item position.
Using the base classes only, things are tough. It's weird anyway, since as soon as you want custom behavior, it would end there without any re-implementations or subclassing. -
@Pl45m4 yes, that's how the nodes-example works: subclassing Items and Scene. So I'm modifying that for my needs and learn on the way what I can...
@herrgross said in QGraphicsSceneMouseEvent not working:
yes, that's how the nodes-example works: subclassing Items and Scene
In 99.9% of the cases, the first thing you do is subclassing the (base-)class of your interest to gain "full control" over it and be able to implement virtual functions, add your own functions / logic or re-implement event handlers for mouse event, for example.
Since Qt is a C++ framework, it is made (and designed) to be used this way. The programmer (Qt user) uses Qt to develop software for the "enduser" (or for education, or whatever purpose). -
@herrgross said in QGraphicsSceneMouseEvent not working:
yes, that's how the nodes-example works: subclassing Items and Scene
In 99.9% of the cases, the first thing you do is subclassing the (base-)class of your interest to gain "full control" over it and be able to implement virtual functions, add your own functions / logic or re-implement event handlers for mouse event, for example.
Since Qt is a C++ framework, it is made (and designed) to be used this way. The programmer (Qt user) uses Qt to develop software for the "enduser" (or for education, or whatever purpose).@Pl45m4 said in QGraphicsSceneMouseEvent not working:
In 99.9% of the cases, the first thing you do is subclassing the (base-)class of your interest to gain "full control" over it and be able to implement virtual functions, add your own functions / logic or re-implement event handlers for mouse event, for example.
@Pl45m4 I just finished some education in C++, but this was hardly a topic. So I am very grateful for this kind of advice - thank's a lot
-