Question about QGraphicsScene and QGraphicsPixmapItem
-
I want to do the following: when I click on the scene, an object Server is added to it at the coordinates of the mouse click and that object I can move.
In an object inherited from QGraphicsPixmapItem, defined mouseMoveEvent methods to move it. Everything is working. When I click on an object and move it with the mouse, the object also moves.
Next, I created a new class inherited from QGraphicsScene and defined the mousePressEvent method in it, which emits a click signal that takes the coordinates of the scene. In the main program, I implemented a slot that adds an object to the scene at the coordinates of the event. Connected signal and slot.
As a result of testing, it turned out that the object is added to the scene when you click on the scene. But it moves only when you double-click.
How do I make it move on one click?class Server : public QGraphicsPixmapItem{ public: Server(QPoint); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); }; void Server::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { this->QGraphicsPixmapItem::setPos(QGraphicsPixmapItem::mapToScene(event->pos()));
class Field : public QGraphicsScene { Q_OBJECT public: Field(); void mousePressEvent(QGraphicsSceneMouseEvent *event); signals: void Press(QPoint); }; void Field::mousePressEvent(QGraphicsSceneMouseEvent *event){ emit Press((event->scenePos().toPoint())); }
scene = new Field; ui->graphicsView->setScene(scene); connect(scene, SIGNAL(Press(QPoint)), this, SLOT(press_on_scene(QPoint))); void MainWindow::press_on_scene(QPoint coordinate){ if(ui->pushButton_2->isChecked()){ Server *serv = new Server(coordinate); scene->addItem(serv); } }
-
@CAP-33
I don't quite follow what you are saying with "But it moves only when you double-click." (I never seem to, unlike some responders here who are clairvoyant!). Are you saying it worked as you wanted to before you created a derived class? [Also yourServer::mouseMoveEvent()
method above is "chopped off".]Anyway, in your
mouseMoveEvent()
overrides you do not call the base implementation. I always do that. Is that your problem? -
@JonB
At first the scene was an object of theQGraphicsScene
class, theQGraphicsScene::mousePressEvent
method was not implemented and the Server moved around the scene with a single mouse click.
After I replaced the scene with an object of theField
class, in which I implemented theField::mousePressEvent method
, the Server can still move around the scene, but for some reason not with one mouse click, but with a double click. -
Hi,
Why not make your item movable ? That way you don't have to do any special handling.
-
@CAP-33 said in Question about QGraphicsScene and QGraphicsPixmapItem:
But it moves only when you double-click.
Could be a focus issue. First click sets focus from scene to item, second activates the click and mouseMovement of your item.
But I would do what @SGaist suggested. If you make your item movable, you shouldn't face any problems -
I solved my problem via event filter of scene.
ui->graphicsView->scene()->installEventFilter(this); bool MainWindow::eventFilter(QObject* obj, QEvent* event){ if(event->type() == QEvent::GraphicsSceneMousePress){ if(ui->addServerBtn->isChecked()){ QGraphicsSceneMouseEvent* mouseSceneEvent; mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event); Server *serv = new Server(mouseSceneEvent->scenePos().toPoint()); scene->addItem((QGraphicsPixmapItem*)serv); } } return false; }