DraggableLine.h 1009 B

123456789101112131415161718192021222324252627282930
  1. #ifndef DRAGGABLELINE_H
  2. #define DRAGGABLELINE_H
  3. #include <QGraphicsLineItem >
  4. #include <QObject>
  5. class DraggableLine : public QGraphicsLineItem {
  6. public:
  7. DraggableLine(const QLineF& line, QGraphicsItem* parent = nullptr)
  8. : QGraphicsLineItem(line, parent), draggingStart(false), draggingEnd(false) {
  9. setAcceptHoverEvents(true);
  10. setFlag(QGraphicsItem::ItemIsSelectable, true);
  11. setFlag(QGraphicsItem::ItemIsMovable, false); // 禁用整体移动,只允许拖动端点
  12. }
  13. protected:
  14. void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
  15. void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
  16. void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
  17. void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override;
  18. private:
  19. bool draggingStart;
  20. bool draggingEnd;
  21. QPointF offset;
  22. bool isNearPoint(const QPointF& pos, const QPointF& point) const;
  23. };
  24. #endif // DRAGGABLELINE_H