#ifndef DRAGGABLELINE_H #define DRAGGABLELINE_H #include <QApplication> #include <QWidget> #include <QPainter> #include <QMouseEvent> #include <QString> #include <QGraphicsView> #include <QGraphicsScene> #include <QGraphicsRectItem> #include <QMouseEvent> #include <QLabel> #include <QPointer> #include <QGraphicsItem> #include <QPainter> #include <QColor> #include <QGraphicsSceneEvent> #include <QDebug> #include <QFrame> // 1. 创建自定义线段类(继承QGraphicsLineItem) class DraggableLine : public QGraphicsLineItem { public: explicit DraggableLine(QGraphicsItem* parent = nullptr) : QGraphicsLineItem(parent) { setFlag(QGraphicsItem::ItemIsMovable, false); // 禁用整体移动 setAcceptHoverEvents(true); } // 2. 鼠标事件处理 protected: void mousePressEvent(QGraphicsSceneMouseEvent* event) override; void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent*) override; // 3. 可视化增强(绘制端点) void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) override ; private: bool draggingStart = false; bool draggingEnd = false; double distance(const QPointF& p1, const QPointF& p2); }; #endif // DRAGGABLELINE_H