Qt鼠标拖动线条组成的多边形移动

实现在鼠标按下线条时可以拖动整个图形的功能,可以按照以下步骤进行:

  1. 记录顶点:定义一个顶点列表,存储多边形的每个顶点。

  2. 检测鼠标按下 :在mousePressEvent中,检测鼠标是否在多边形的任意一条边上。

  3. 记录按下的顶点:如果鼠标在线上,记录下被按下的顶点的索引。

  4. 计算偏移量:在鼠标按下时,计算鼠标位置和被按下顶点之间的偏移量。

  5. 移动顶点 :在mouseMoveEvent中,如果鼠标按钮保持按下状态,则根据鼠标的当前位置和之前计算的偏移量更新顶点的位置。

  6. 重绘图形 :在顶点移动后,调用update方法来请求重绘控件。

  7. 结束拖动 :在mouseReleaseEvent中,结束拖动操作。

    cpp 复制代码
    #include <QWidget>
    #include <QMouseEvent>
    #include <QPainter>
    #include <QVector>
    #include <QPointF>
    #include <cmath>
    
    class MovablePolygon : public QWidget {
        Q_OBJECT
    
    public:
        MovablePolygon(QWidget *parent = nullptr) : QWidget(parent),
            dragging(false), currentPointIndex(-1), dragOffset(0, 0) {
            // 初始化多边形顶点
            points << QPointF(50, 50) << QPointF(150, 50)
                    << QPointF(150, 150) << QPointF(50, 150);
        }
    
    protected:
        void paintEvent(QPaintEvent *event) override {
            QPainter painter(this);
            painter.setPen(QPen(Qt::black, 2));
            for (int i = 0; i < points.size() - 1; ++i) {
                painter.drawLine(points[i], points[i + 1]);
            }
            // 闭合多边形
            painter.drawLine(points.last(), points.first());
        }
    
        void mousePressEvent(QMouseEvent *event) override {
            if (event->button() == Qt::LeftButton) {
                dragging = true;
                for (int i = 0; i < points.size() - 1; ++i) {
                    if (isPointOnLine(event->pos(), points[i], points[i + 1], 10)) {
                        currentPointIndex = i;
                        dragOffset = event->pos() - points[i];
                        break;
                    }
                }
                if (currentPointIndex == -1) {
                    // 如果没有点被按下,可能需要移动整个多边形
                    dragOffset = event->pos() - points[0];
                }
            }
        }
    
        void mouseMoveEvent(QMouseEvent *event) override {
            if (dragging && event->buttons() & Qt::LeftButton) {
                if (currentPointIndex != -1) {
                    points[currentPointIndex] = event->pos() - dragOffset;
                } else {
                    // 移动整个多边形
                    for (int i = 0; i < points.size(); ++i) {
                        points[i] += event->pos() - dragOffset;
                    }
                }
                update(); // 重绘控件
            }
        }
    
        void mouseReleaseEvent(QMouseEvent *event) override {
            if (event->button() == Qt::LeftButton) {
                dragging = false;
                currentPointIndex = -1;
            }
        }
    
    private:
        bool dragging;
        int currentPointIndex;
        QPoint dragOffset;
        QVector<QPointF> points;
    
        bool isPointOnLine(const QPointF &point, const QPointF &lineStart, const QPointF &lineEnd, double tolerance) {
            double dist = qAbs((lineEnd.y() - lineStart.y()) * point.x()
                                - (lineEnd.x() - lineStart.x()) * point.y()
                                + lineEnd.x() * lineStart.y()
                                - lineEnd.y() * lineStart.x());
            double lineLength = QLineF(lineStart, lineEnd).length();
            return dist <= tolerance && lineLength >= qAbs(point.x() - lineStart.x());
        }
    };
    
    #include "MovablePolygon.moc" // 确保包含moc文件

    在这个示例中,MovablePolygon类是一个自定义的QWidget,它包含一个多边形顶点列表points。我们重写了paintEvent来绘制多边形,重写了鼠标事件来处理拖动逻辑。isPointOnLine函数用于检测鼠标点击是否在多边形的边上,如果检测到鼠标在线上,就记录下顶点索引和偏移量,并在鼠标移动时更新顶点位置。

相关推荐
普贤莲花6 分钟前
先学Workbuddy还是“人工智能训练师”,或者python基础课?
开发语言·人工智能·python
星之擎8 分钟前
Task 很优雅的取消方式是什么?C# 里面。
开发语言·c#
努力努力再努力wz19 分钟前
【C++ 高阶系列】C++ 高性能网络编程:当 Reactor 遇见协程:从 epoll 回调到异步 MySQL,彻底打通任务挂起与恢复
网络·c++·mysql
FRing0328 分钟前
JavaScript入门到精通二十八(forEach遍历、创建对象的三种方式、构造函数、new实例化执行过程、实例成员和静态成员)
开发语言·前端·javascript
1024奇点1 小时前
【仓颉语言入门 · 第二篇 第 15 课】
开发语言·数据库·ide·开源
霍霍的袁1 小时前
【C++11】左值右值引用与移动语义详细笔记(左值右值、右值引用、移动构造、移动赋值、引用折叠、万能转发)
c++·笔记·学习·visual studio
独隅1 小时前
Claude 高效使用指南深度评测
java·开发语言·claude·claude code
Agent世界1 小时前
千问Go上线我的应用:一句话生成可部署 AI 软件的技术拆解
开发语言·人工智能·golang·ai智能体
布莱克6051 小时前
C++ 中 unordered_set 与 unordered_map 有什么不同?一篇讲透
开发语言·c++·哈希
小豆庄1 小时前
视频脱敏引擎的追踪难题:转场误判定、ID撞号和淡入窗口
qt·算法·目标检测·音视频·视频·敏捷流程