1
votes

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.

Application image

1
The items that are selected are the circles and the circles to join must belong to different ports. I am right?eyllanesc
@eyllanesc Yes, you are rightBrykyz
The blocks are always made up of a rectangle and 3 circles in the topLeft?eyllanesc
@eyllanesc For now yes, there can be one to five portsBrykyz
I think we need more info on this. Are these shapes (rect and circles) aggregated inside a custom QGraphicsItem derived class? Show us the implementation. I also assume you have your own class which inherits from QGraphicsView. Is this correct? Is it where you handle mouse events?Dusteh

1 Answers

2
votes

I solved this with

QGraphicsItem *item = scene.itemAt(mapToScene(event->pos()), QTransform());

in function mouseReleaseEvent