I am trying to connect two QGraphicsItems
by straight line (QGraphicsLineItem
); by clicking with middle mouse button on first object, then hover on second object and after I release it, it should draw line between them.
This is function that should connect these items.
void GraphicsBlock::connectBlocks(GraphicsBlock *block)
{
GraphicsConnect *connection = new GraphicsConnect(); //Class with QGraphicsLineItem
connection->line->setLine(QLineF(this->pos(), block->pos()));
}
I am working on one scene and I've got problem with finding second block by mouse position. Function mousePressEvent
should work with this code:
if(event->button() == Qt::MiddleButton)
Connecting == true;
and after that mouseReleaseEvent
if(Connecting)
{
//Get object by mouse position here
//this.connectBlocks(..)
Connecting = false;
}
I tried scene->itemsAt(mouse.x(), mouse.y(), QTransform())
and
scene->items(QPointF(mouse.x(),mouse.y())
but it always returned empty list.
EDIT: This is how application should work, blue ellipse is clicked port, now i should drag (still mouse down) to another port and release mouse.
I solved this with
QGraphicsItem *item = scene.itemAt(mapToScene(event->pos()), QTransform());
in function mouseReleaseEvent