Hello there,
I was looking for a plotting tool, which can use Touch, Multitouch and Gestures. Thanks to QCustomPlot that it is open source!
For all those, who are looking for QCustomPlot with touch functionality, here is my workaround:
1. Create a new value within the private section of QCustomPlot:
private: QPointF currentTouchPointPos;
2. Extend the constructor of QCustomPlot:
setAttribute( Qt::WA_AcceptTouchEvents ); grabGesture( Qt::PinchGesture );
3. Overwrite the standard event-handler of QWidget within the QCustomPlot:
bool QCustomPlot::event( QEvent *event ){ switch( event->type() ){ case QEvent::Gesture: { QGestureEvent *gestureEve = static_cast<QGestureEvent*>(event); if( QGesture *pinch = gestureEve->gesture(Qt::PinchGesture) ){ QPinchGesture *pinchEve = static_cast<QPinchGesture *>(pinch); qreal scaleFactor = pinchEve->totalScaleFactor( ); if( scaleFactor > 1.0 ){ scaleFactor *= 10; }else{ scaleFactor *= -10; } QWheelEvent *wheelEve = new QWheelEvent( currentTouchPointPos, scaleFactor, Qt::NoButton, Qt::NoModifier, Qt::Vertical ); this->wheelEvent( wheelEve ); } return true; } case QEvent::TouchBegin: case QEvent::TouchUpdate: case QEvent::TouchEnd: { QTouchEvent *touchEvent = static_cast<QTouchEvent *>( event ); QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints( ); if( touchPoints.count( ) == 1 ){ const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first( ); currentTouchPointPos = touchPoint0.pos(); QMouseEvent *mouseEve = new QMouseEvent(QEvent::MouseButtonPress,currentTouchPointPos,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier); if( touchEvent->touchPointStates() == (Qt::TouchPointStates)Qt::TouchPointPressed ){ this->mousePressEvent( mouseEve ); }else if( touchEvent->touchPointStates() == (Qt::TouchPointStates)Qt::TouchPointMoved ){ this->mouseMoveEvent( mouseEve ); }else if( touchEvent->touchPointStates() == (Qt::TouchPointStates)Qt::TouchPointReleased ){ this->mouseReleaseEvent( mouseEve ); } } return true; } default: { break; } } return QWidget::event( event ); }
4. If you don't want to use the wheelevent for the pinch gesture you can also use this code snippet:
qreal scaleFactor = pinchEve->totalScaleFactor( ); if( scaleFactor > 1.0 ){ scaleFactor = fabs(1.0 - scaleFactor) - 1.0; }else if( scaleFactor < 1.0 ){ scaleFactor = fabs(1.0 - scaleFactor) + 1.0; } if( scaleFactor < 0.98 ) scaleFactor = 0.98; else if( scaleFactor > 1.02 ) scaleFactor = 1.02; this->yAxis->scaleRange( scaleFactor, this->yAxis->range().center()); this->xAxis->scaleRange( scaleFactor, this->xAxis->range().center()); this->replot();
Hopefully that helps some people to get into it, because I've found nothing in the forum/comments about using QCustomPlot with touch.
Other ideas are welcome at any time.
Greetings,
Mike