Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Qt for android觸摸手勢事件QGestureEvent

Qt for android觸摸手勢事件QGestureEvent

編輯:關於Android編程

在觸摸設備上可以使用Qt的手勢事件

要激活手勢事件,需要執行以下操作:

第一步,為QWidget控件注冊手勢事件

QList 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(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(swipe));
    else if (QGesture *pan = event->gesture(Qt::PanGesture))
        panTriggered(static_cast(pan));
    else if (QGesture *tap = event->gesture(Qt::TapGesture))
        tapTriggered(static_cast(tap));
    else if (QGesture *tapAndHold = event->gesture(Qt::TapAndHoldGesture))
        tapAndHoldTriggered(static_cast(tapAndHold));
    if (QGesture *pinch = event->gesture(Qt::PinchGesture))
        pinchTriggered(static_cast(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():;

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved