Update: I have tried two things without avail:
1. after creating the object calling "grabMouse()" on the object (still within the QGraphicsScene::mousePressEvent() event handler function). Does not work because base class implementation ungrabs the item right away.
2. sending an own MousePressEvent to the graphics view using the following code:
QPointF ptScene
= mouseEvent
->pos
();
// mouseEvent->pos() holds the current position that the user clicked on // Note: the newly created item has a boundingRect() that encloses this position.
QPoint ptView
= view
->mapFromScene
(ptScene
);
QPoint ptGlobal
= view
->viewport
()->mapToGlobal
(ptView
);
event2.setScenePos(ptScene);
event2.setPos(ptScene);
event2.setScreenPos(ptGlobal);
event2.setButton(Qt::LeftButton);
event2.setButtons(Qt::LeftButton);
qApp->sendEvent(view, &event2); // strange: does not call the QGraphicsScene::mousePressEvent() function again!
QGraphicsView* view = views()[0];
QPointF ptScene = mouseEvent->pos(); // mouseEvent->pos() holds the current position that the user clicked on
// Note: the newly created item has a boundingRect() that encloses this position.
QPoint ptView = view->mapFromScene(ptScene);
QPoint ptGlobal = view->viewport()->mapToGlobal(ptView);
QGraphicsSceneMouseEvent event2(QEvent::GraphicsSceneMousePress);
event2.setScenePos(ptScene);
event2.setPos(ptScene);
event2.setScreenPos(ptGlobal);
event2.setButton(Qt::LeftButton);
event2.setButtons(Qt::LeftButton);
event2.setModifiers(QApplication::keyboardModifiers());
qApp->sendEvent(view, &event2); // strange: does not call the QGraphicsScene::mousePressEvent() function again!
To copy to clipboard, switch view to plain text mode
So, in variant 2, maybe the mouse press event object is not fully configured? Behavior does not change if I delay sending the event by a timer.
Problem remains: creating an object on click and putting it into drag mode right away...???
Added after 21 minutes:
Solved:
1. create new item
2. set flags: setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
3. add to scene
4. call grabMouse() on newly created item
Do not send another event!
This appears to work.
Note: you may call setSelected(true) when creating the item as well. However, on first move the item will get selected anyway.
Bookmarks