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

相关推荐
Hylan_J2 小时前
【VSCode】MicroPython环境配置
ide·vscode·python·编辑器
软件黑马王子2 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
cpp_learners2 小时前
QT 引入Quazip和Zlib源码工程到项目中,无需编译成库,跨平台,压缩进度
qt·zlib·加密压缩·quazip
闲猫2 小时前
go orm GORM
开发语言·后端·golang
黑不溜秋的3 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
李白同学3 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?4 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农5 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿5 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!5 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode