本应用于基于QGraphicsView框架,实现多点触摸. 工程仅仅演示了多点触摸绘图,源自我前段时间一款基于Qt的绘图软件.框架
工程结构:ide
kmp.h 定义了枚举工具
slide.h/cpp 定义了派生于QGraphicsScene的slide类,实现绘制的主要功能this
widget.h/cpp 定义了派生于QGraphicsView的widget类,多点了多点触摸部分.spa
kmpinkelement.h/cpp 定义了派生于QGraphicsPathItem的笔迹对象.code
应用实现了抒写,没有实现相似其余工具:橡皮擦,选择工具,漫游工具等对象
QGraphicsView的多点触摸在viewportEvent事件,处理TouchBegin/touchUpdate/TouchEnd事件,在处理touch事件中须要经过判断每一个点的状态,同时根据每一个点的ID来实现多点笔迹的管理.blog
bool Widget::viewportEvent(QEvent *event){ // 处理touch事件 QEvent::Type evType = event->type(); if(evType==QEvent::TouchBegin || evType == QEvent::TouchUpdate || evType == QEvent::TouchEnd ) { QTouchEvent* touchEvent = static_cast<QTouchEvent*>(event); QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints(); foreach( const QTouchEvent::TouchPoint tp , touchPoints ){ //不考虑pad QPoint touchPos = QPoint( tp.pos().x() , tp.pos().y() ); if(tp.id() == 0 ){ if( tp.state() == Qt::TouchPointPressed ) this->_isTouchMode = true; else this->_isTouchMode = false; } QPointF scenepos = this->mapToScene(touchPos.x() , touchPos.y() ); switch( tp.state() ){ case Qt::TouchPointPressed: this->_currentSlide->onDeviceDown(scenepos, tp.id()); break; case Qt::TouchPointMoved: this->_currentSlide->onDeviceMove(scenepos,tp.id()); break; case Qt::TouchPointReleased: this->_currentSlide->onDeviceUp(tp.id()); break; } } if(evType == QEvent::TouchEnd ){ // to do } return true; } return QGraphicsView::viewportEvent(event); }
书写咱们是基于QGraphiscLineItem的,书写结束后咱们才生成KMPInkElement,因此在slide类中咱们看到咱们有一个std::vector集合来存储绘制过程当中添加产生的QGraphicsLineItem对象,在最后结束绘制后须要画板上移除集合中全部对象。事件
具体能够查看源码,没有太多复杂的东西element
开发环境: QT5.5 , QtCreator , win7