Qt区分鼠标按下时移动的是哪个多边形

  1. 使用不同的鼠标事件处理器:为每个多边形分配不同的事件处理器,或者在同一个处理器中使用逻辑来区分。

  2. 检查鼠标点击位置:在鼠标按下事件中,检查鼠标的位置是否在某个多边形的边上或顶点上。

  3. 使用图形的标识符:给每个多边形分配一个唯一的标识符,并在鼠标事件中使用这个标识符来识别多边形。

  4. 利用图形的属性:例如,可以使用图形的颜色、层级或其他属性作为区分依据。

    cpp 复制代码
    #include <QWidget>
    #include <QMouseEvent>
    #include <QPainter>
    #include <QVector>
    #include <QPointF>
    #include <QLineF>
    #include <cmath>
    
    class PolyWidget : public QWidget {
        Q_OBJECT
    
    public:
        PolyWidget(QWidget *parent = nullptr) : QWidget(parent) {
            // 初始化两个多边形
            polygon1 << QPointF(50, 50) << QPointF(150, 50) << QPointF(100, 150);
            polygon2 << QPointF(200, 100) << QPointF(300, 100) << QPointF(250, 200);
        }
    
    protected:
        void paintEvent(QPaintEvent *event) override {
            QPainter painter(this);
            painter.setPen(QPen(Qt::black, 2));
            
            // 绘制第一个多边形
            drawPolygon(painter, polygon1, 1);
            // 绘制第二个多边形
            drawPolygon(painter, polygon2, 2);
        }
    
        void mousePressEvent(QMouseEvent *event) override {
            if (event->button() == Qt::LeftButton) {
                if (isInsidePolygon(polygon1, event->pos())) {
                    selectedPolygon = 1;
                } else if (isInsidePolygon(polygon2, event->pos())) {
                    selectedPolygon = 2;
                }
            }
        }
    
        void mouseMoveEvent(QMouseEvent *event) override {
            if (selectedPolygon == 1 && event->buttons() & Qt::LeftButton) {
                movePolygon(polygon1, event->pos());
            } else if (selectedPolygon == 2 && event->buttons() & Qt::LeftButton) {
                movePolygon(polygon2, event->pos());
            }
            update();
        }
    
        void mouseReleaseEvent(QMouseEvent *event) override {
            selectedPolygon = 0; // 重置选中的多边形
        }
    
    private:
        QVector<QPointF> polygon1;
        QVector<QPointF> polygon2;
        int selectedPolygon = 0; // 0 表示没有多边形被选中
    
        void drawPolygon(QPainter &painter, const QVector<QPointF> &polygon, int id) {
            for (int i = 0; i < polygon.size() - 1; ++i) {
                painter.drawLine(polygon[i], polygon[i + 1]);
            }
            painter.drawLine(polygon.last(), polygon.first()); // 闭合多边形
            // 可以在这里添加更多与 id 相关的绘制逻辑
        }
    
        bool isInsidePolygon(const QVector<QPointF> &polygon, const QPointF &pos) {
            // 这里实现检测点是否在多边形内部的逻辑
            // 可以使用射线法或多边形的边界框来优化检测
            // 以下代码仅为示例,实际检测逻辑可能更复杂
            for (int i = 0; i < polygon.size() - 1; ++i) {
                QLineF line(polygon[i], polygon[i + 1]);
                if (line.intersects(QLineF(pos, pos + QPointF(1, 0)))) {
                    return true;
                }
            }
            return false;
        }
    
        void movePolygon(QVector<QPointF> &polygon, const QPointF &newPos) {
            if (selectedPolygon) {
                QPointF delta = newPos - polygon.first();
                for (QPointF &point : polygon) {
                    point += delta;
                }
            }
        }
    };
    
    #include "PolyWidget.moc" // 确保包含moc文件

    在这个示例中,PolyWidget类包含两个多边形polygon1和polygon2。我们重写了paintEvent来绘制这两个多边形,并重写了鼠标事件来处理拖动逻辑。

  5. mousePressEvent:当鼠标按下时,使用isInsidePolygon函数检查鼠标点击是否在任一个多边形的边上或顶点上,根据检测结果设置selectedPolygon。

  6. mouseMoveEvent:当鼠标移动时,如果左键保持按下状态,并且selectedPolygon不为0,则调用movePolygon函数移动当前选中的多边形。

  7. mouseReleaseEvent:当鼠标左键释放时,重置selectedPolygon。

相关推荐
会编程的小孩6 小时前
C++ qt的安装
c++
小成2023032026512 小时前
C++~07补充
开发语言·c++
hold?fish:palm12 小时前
40 二叉树的直径
c++·leetcode
LaoWaiHang12 小时前
关于Qt中鼠标左键事件被子控件截留的处理
qt
解决小子12 小时前
【虚幻引擎】【C++】【GAS框架】构建角色基类、属性集、数值钳制
c++·笔记·游戏引擎·虚幻·gas·能力系统
lunar_coder14 小时前
【C++】如何解决智能指针的析构函数,如何实现shared_ptr的引用计数以及解决shared_ptr的循环引用
c++·1024程序员节
白白白小纯14 小时前
C++进阶.多态
c++
凯瑟琳.奥古斯特15 小时前
力扣1016高效解法:剪枝优化技巧
开发语言·c++·算法·leetcode·职场和发展
xiangyun6116 小时前
C++ priority_queue 底层:手撕二叉堆与 Top K 问题
c++·算法·面试·stl·1024程序员节
坤坤子吖16 小时前
C++继承:友元、静态成员、多继承与组合
开发语言·c++·笔记·学习·1024程序员节