Qt使用触屏对图片进行平移和缩放

15 篇文章 1 订阅

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 );
	
}

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值