模拟鼠标事件
1、模拟鼠标按下事件
cpp
QPoint p = this->rect().center();
QMouseEvent *pressEvent = new QMouseEvent(QEvent::MouseButtonPress,
p,
Qt::LeftButton,
Qt::LeftButton,
Qt::NoModifier);
mousePressEvent(pressEvent);
2、模拟鼠标松开事件
cpp
QMouseEvent *releaseEvent = new QMouseEvent(QEvent::MouseButtonRelease,
p,
Qt::LeftButton,
Qt::LeftButton,
Qt::NoModifier);
mouseReleaseEvent(releaseEvent);
3、模拟鼠标点击事件
按顺序模拟1、2的事件,代码如下:
cpp
mousePressEvent(pressEvent);
mouseReleaseEvent(releaseEvent);
4、模拟鼠标移动事件
cpp
QMouseEvent *moveEvent = new QMouseEvent(QEvent::MouseMove,
p,
Qt::LeftButton,
Qt::LeftButton,
Qt::NoModifier);
mousePressEvent(pressEvent);
mouseReleaseEvent(releaseEvent);
mouseMoveEvent(moveEvent); //新增移动事件刷新画线和十字线