Qt鼠标拖动线条组成的多边形移动
创始人
2025-01-10 04:36:46
0

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

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

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

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

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

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

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

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

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

相关内容

热门资讯

技巧辅助挂!pokermast... 技巧辅助挂!pokermaster修改器,丹东约战麻将辅助器,演示教程(有挂细节)1、点击下载安装,...
现场直击!wepokerplu... 现场直击!wepokerplus万能挂,丰城双剑新版最强高分攻略,操作教程(有挂方针)1.丰城双剑新...
插件辅助挂!wepoker有辅... 插件辅助挂!wepoker有辅助器吗,乐平包王攻略,学习教程(有挂方略)1、首先打开乐平包王攻略辅助...
据玩家消息!拱趴大菠萝辅助神器... 据玩家消息!拱趴大菠萝辅助神器,多乐跑得快辅助器,机巧教程(证实有挂)1、在拱趴大菠萝辅助神器插件功...
此事备受玩家关注!来玩app破... 此事备受玩家关注!来玩app破解版,h5能反杀吗,绝活教程(有挂详细)1、打开软件启动之后找到中间准...
值得注意的是!aapoker破... 值得注意的是!aapoker破解侠是真的吗,蜀山四川游戏修改工具,经验教程(有挂助手)1、金币登录送...
第三方辅助!wepoker脚本... 第三方辅助!wepoker脚本,广东星悦有外开挂辅助器吗,法门教程(有挂分析)广东星悦有外开挂辅助器...
此事引发广泛关注!德州透视脚本... 此事引发广泛关注!德州透视脚本,崇阳斗棋辅助脚本视频,诀窍教程(的确有挂)暗藏猫腻,小编详细说明崇阳...
黑科技辅助挂!wepoker买... 黑科技辅助挂!wepoker买脚本靠谱吗,情怀七喜游戏辅助,法门教程(有挂方法)1、每一步都需要思考...
方法辅助挂!aapoker怎么... 方法辅助挂!aapoker怎么设置提高好牌几率,蘑菇云辅助使用视频,绝活儿教程(讲解有挂)1、完成蘑...