本應用於基於QGraphicsView框架,實現多點觸摸. 工程僅僅演示了多點觸摸繪圖,源自我前段時間一款基於Qt的繪圖軟件.
工程結構:

kmp.h 定義了枚舉
slide.h/cpp 定義了派生於QGraphicsScene的slide類,實現繪制的主要功能
widget.h/cpp 定義了派生於QGraphicsView的widget類,多點了多點觸摸部分.
kmpinkelement.h/cpp 定義了派生於QGraphicsPathItem的筆跡對象.
應用實現了抒寫,沒有實現類似其他工具:橡皮擦,選擇工具,漫游工具等
QGraphicsView的多點觸摸在viewportEvent事件,處理TouchBegin/touchUpdate/TouchEnd事件,在處理touch事件中需要通過判斷每個點的狀態,同時根據每個點的ID來實現多點筆跡的管理.
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對象,在最后結束繪制后需要畫板上移除集合中所有對象。
具體可以查看源碼,沒有太多復雜的東西
開發環境: QT5.5 , QtCreator , win7
