1、对需要响应触屏的窗口 激活触屏事件
//使用触屏的窗口激活触屏事件
setAttribute(Qt::WA_AcceptTouchEvents,true);
2、重写event函数
//在 bool event( QEvent* e); 中过滤
bool XXXTouchWidget::event( QEvent* e ) /*override*/
{
switch( e ->type( ) )
{
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
{
auto ouchEvent = static_cast<QTouchEvent *>(event);
auto&& touchPoints = touchEvent->touchPoints();
if (touchPoints.count() == 2)
{
// 有2个手指触屏
// determine scale factor
const auto& touchPoint0 = touchPoints.first();
const auto& touchPoint1 = touchPoints.last();
auto currentScaleFactor =
QLineF(touchPoint0.pos(), touchPoint1.pos()).length()
/ QLineF(touchPoint0.startPos(), touchPoint1.startPos()).length();
/// 计算当前的缩放比
auto curZoom = m_lastZoom * currentScaleFactorl
if (touchEvent->touchPointStates() & Qt::TouchPointReleased)
{
/// 如果释放触点,保存上次的缩放比
zoomCoef( currentScaleFactor );
}
}
else if( touchPoints.size() == 1 )
{
/// 仅一个触点(手指)按下
const atuo &touchPoint0 = touchPoints.first();
const auto& pos = touchPoint0.pos();
const auto& posLast = touchPoint0.lastPos();
const auto& posDelta = pos - posLast;
auto dx = int(posDelta.x());
auto dy = int(posDelta.y());
if(dx != 0 || dy != 0 )
{
/// 有效的平移系数(delta值)
/// 使用dx和dy进行位置平移
}
}
// 如果不许好后续处理
// e ->accept( true );
default:
// 其他的事件
break;
}
return __super::event( e );
}