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函数用于检测鼠标点击是否在多边形的边上,如果检测到鼠标在线上,就记录下顶点索引和偏移量,并在鼠标移动时更新顶点位置。

相关推荐
Lizhihao_3 分钟前
JAVA-堆 和 堆排序
java·开发语言
极客先躯8 分钟前
高级java每日一道面试题-2025年3月21日-微服务篇[Nacos篇]-什么是Nacos?
java·开发语言·微服务
BC橡木17 分钟前
C++ IO流
c++
try again!22 分钟前
rollup.js 和 webpack
开发语言·javascript·webpack
du fei33 分钟前
C# 窗体应用(.FET Framework) 线程操作方法
开发语言·c#
du fei35 分钟前
C#文件操作
开发语言·c#
m0_5557629044 分钟前
struct 中在c++ 和c中用法区别
java·c语言·c++
月亮有痕迹诶1 小时前
【C++】智能指针
开发语言·c++·c++11
搞不懂语言的程序员1 小时前
装饰器模式详解
开发语言·python·装饰器模式
王禄DUT1 小时前
化学方程式配平 第33次CCF-CSP计算机软件能力认证
开发语言·c++·算法