首先设置接收触控事件 setAttribute(Qt::WA_AcceptTouchEvents, true);
bool CollectionPoint::event(QEvent *event)
{
switch (event->type()) {
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
{
m_pos.clear();
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
for (int i = 0; i < touchPoints.count(); i++)
{
const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.at(i);
QPoint point0 = mapToGlobal(touchPoint0.pos().toPoint());
POINTINFO info;
info.id = QString::number(i);
info.x = touchPoint0.pos().toPoint().x();
info.y = touchPoint0.pos().toPoint().y();
switch (touchPoints.at(i).state())
{
case Qt::TouchPointPressed:
{
info.iState = 7;
}
break;
case Qt::TouchPointMoved:
{
info.iState = 7;
}
break;
case Qt::TouchPointReleased:
{
info.iState = 6;
}
break;
default:
break;
}
m_pos.append(info);
}
update();
sendPos();
return true;
}
default:
break;
}
return QWidget::event(event);
}