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类包含两个多边形polygon1polygon2。我们重写了paintEvent来绘制这两个多边形,并重写了鼠标事件来处理拖动逻辑。

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

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

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

相关推荐
lxmyzzs5 小时前
pyqt5无法显示opencv绘制文本和掩码信息
python·qt·opencv
大橘5 小时前
【qml-4】qml与c++交互(类型多例)
qt·qml
mit6.8245 小时前
[openvela] Hello World :从零开始的完整实践与问题复盘
c++·嵌入式硬件
啊阿狸不会拉杆7 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
小学生的信奥之路7 小时前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
曙曙学编程8 小时前
stm32——GPIO
c语言·c++·stm32·单片机·嵌入式硬件
△曉風殘月〆9 小时前
Visual Studio中的常用调试功能(下)
c++·ide·visual studio·调试
武当豆豆9 小时前
C++编程学习(第25天)
开发语言·c++·学习
minji...12 小时前
C++ string类(STL简介 , string类 , 访问修改字符)
开发语言·c++
Forward♞12 小时前
Qt——文件操作
开发语言·c++·qt