码迷,mamicode.com
首页 > 移动开发 > 详细

Qt for android触摸手势事件QGestureEvent

时间:2014-07-12 20:30:44      阅读:807      评论:0      收藏:0      [点我收藏+]

标签:qt   android   手势事件   

在触摸设备上可以使用Qt的手势事件

要激活手势事件,需要执行以下操作:

第一步,为QWidget控件注册手势事件

QList<Qt::GestureType> gestures;
    gestures << Qt::PanGesture;
    gestures << Qt::PinchGesture;
    gestures << Qt::SwipeGesture;
    gestures << Qt::TapGesture;
    gestures << Qt::TapAndHoldGesture;
    MainWindow w;
    w.grabGestures(gestures);
 
第二步,在QWidget控件类中过滤事件
bool MainWindow::event(QEvent *event)
{
    if (event->type() == QEvent::Gesture)
        return gestureEvent(static_cast<QGestureEvent*>(event));
    return QWidget::event(event);
}
bool MainWindow::gestureEvent(QGestureEvent *event)
{
    qCDebug(lcExample) << "gestureEvent():" << event->gestures().size();
    if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
        swipeTriggered(static_cast<QSwipeGesture *>(swipe));
    else if (QGesture *pan = event->gesture(Qt::PanGesture))
        panTriggered(static_cast<QPanGesture *>(pan));
    else if (QGesture *tap = event->gesture(Qt::TapGesture))
        tapTriggered(static_cast<QTapGesture *>(tap));
    else if (QGesture *tapAndHold = event->gesture(Qt::TapAndHoldGesture))
        tapAndHoldTriggered(static_cast<QTapAndHoldGesture *>(tapAndHold));
    if (QGesture *pinch = event->gesture(Qt::PinchGesture))
        pinchTriggered(static_cast<QPinchGesture *>(pinch));
    return true;
}
//! [gesture event handler]
void MainWindow::panTriggered(QPanGesture *gesture)
{
#ifndef QT_NO_CURSOR
    switch (gesture->state()) {
        case Qt::GestureStarted:
        case Qt::GestureUpdated:
            setCursor(Qt::SizeAllCursor);
            break;
        default:
            setCursor(Qt::ArrowCursor);
    }
#endif
    QPointF delta = gesture->delta();
    qCDebug(lcExample) << "panTriggered():" << delta;
    horizontalOffset += delta.x();
    verticalOffset += delta.y();
}
void MainWindow::pinchTriggered(QPinchGesture *gesture)
{
    QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();
    if (changeFlags & QPinchGesture::RotationAngleChanged) {
        const qreal value = gesture->property("rotationAngle").toReal();
        const qreal lastValue = gesture->property("lastRotationAngle").toReal();
        const qreal rotationAngleDelta = value - lastValue;
        rotationAngle += rotationAngleDelta;
        qCDebug(lcExample) << "pinchTriggered(): rotation by" << rotationAngleDelta << rotationAngle;
    }
    if (changeFlags & QPinchGesture::ScaleFactorChanged) {
        qreal value = gesture->property("scaleFactor").toReal();
        currentStepScaleFactor = value;
        qCDebug(lcExample) << "pinchTriggered(): " << currentStepScaleFactor;
    }
    if (gesture->state() == Qt::GestureFinished) {
        scaleFactor *= currentStepScaleFactor;
        currentStepScaleFactor = 1;
    }
    update();
}
//! [swipe function]
void MainWindow::swipeTriggered(QSwipeGesture *gesture)
{
    if (gesture->state() == Qt::GestureFinished) {
        if (gesture->horizontalDirection() == QSwipeGesture::Left
            || gesture->verticalDirection() == QSwipeGesture::Up) {
            qCDebug(lcExample) << "swipeTriggered(): swipe to previous";
        } else {
            qCDebug(lcExample) << "swipeTriggered(): swipe to next";
        }
    }
}
//! [swipe function]
void MainWindow::tapTriggered(QTapGesture *gesture)
{
    qCDebug(lcExample) << "tapTriggered():" ;
}
void MainWindow::tapAndHoldTriggered(QTapAndHoldGesture *gesture)
{
    qCDebug(lcExample) << "tapAndHoldTriggered():";
}

 

参考源代码 http://download.csdn.net/detail/liuguangzhou123/7616623

Qt for android触摸手势事件QGestureEvent,布布扣,bubuko.com

Qt for android触摸手势事件QGestureEvent

标签:qt   android   手势事件   

原文地址:http://blog.csdn.net/liuguangzhou123/article/details/37656495

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!