-
使用不同的鼠标事件处理器:为每个多边形分配不同的事件处理器,或者在同一个处理器中使用逻辑来区分。
-
检查鼠标点击位置:在鼠标按下事件中,检查鼠标的位置是否在某个多边形的边上或顶点上。
-
使用图形的标识符:给每个多边形分配一个唯一的标识符,并在鼠标事件中使用这个标识符来识别多边形。
-
利用图形的属性:例如,可以使用图形的颜色、层级或其他属性作为区分依据。
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来绘制这两个多边形,并重写了鼠标事件来处理拖动逻辑。 -
mousePressEvent:当鼠标按下时,使用isInsidePolygon函数检查鼠标点击是否在任一个多边形的边上或顶点上,根据检测结果设置selectedPolygon。 -
mouseMoveEvent:当鼠标移动时,如果左键保持按下状态,并且selectedPolygon不为0,则调用movePolygon函数移动当前选中的多边形。 -
mouseReleaseEvent:当鼠标左键释放时,重置selectedPolygon。
Qt区分鼠标按下时移动的是哪个多边形
房大豪 C++ Qt2024-07-18 16:41
相关推荐
xcyxiner8 小时前
DicomViewer (添加模型类)3xcyxiner1 天前
DicomViewer (目录调整) 2xcyxiner1 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2clint4562 天前
C++进阶(1)——前景提要夜悊2 天前
C++代码示例:进制数简单生成工具郝学胜_神的一滴2 天前
CMake 021: IF 条件判据详诠_wyt0013 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解玖玥拾3 天前
C/C++ 数据结构(七)栈、容器适配器桥田智能3 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案один but you3 天前
constexpr函数