#ifndef DRAGGABLELINE_H #define DRAGGABLELINE_H #include #include class DraggableLine : public QGraphicsLineItem { public: DraggableLine(const QLineF& line, QGraphicsItem* parent = nullptr) : QGraphicsLineItem(line, parent), draggingStart(false), draggingEnd(false) { setAcceptHoverEvents(true); setFlag(QGraphicsItem::ItemIsSelectable, true); setFlag(QGraphicsItem::ItemIsMovable, false); // 禁用整体移动,只允许拖动端点 } protected: void mousePressEvent(QGraphicsSceneMouseEvent* event) override; void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override; void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; private: bool draggingStart; bool draggingEnd; QPointF offset; bool isNearPoint(const QPointF& pos, const QPointF& point) const; }; #endif // DRAGGABLELINE_H