#include "connection.h" #include #include Connection::Connection(Item* from, Item::AnchorType fromType, Item* to, Item::AnchorType toType, QGraphicsItem* parent) : QGraphicsLineItem(parent), from_(from), to_(to), fromType_(fromType), toType_(toType) { setZValue(-1); // 在图元之下 setPen(QPen(Qt::black, 2)); setFlags(QGraphicsItem::ItemIsSelectable); from_->addConnection(this); to_->addConnection(this); updatePosition(); } void Connection::updatePosition() { setLine(QLineF(from_->anchorPos(fromType_), to_->anchorPos(toType_))); } void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { QPen pen = this->pen(); if (isSelected()) { pen.setColor(Qt::red); // 选中变红 pen.setWidth(3); } painter->setPen(pen); painter->drawLine(line()); }