Qt Swipe手势无法正常工作

时间:2015-12-04 10:05:38

标签: c++ qt swipe gestures

我正在尝试在我的小部件中启用滑动手势,但遗憾的是它无效。 我按照Qt。

提供的文档和示例进行了操作

以下是我的代码的一部分:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    grabGesture(Qt::SwipeGesture);
}

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){
    if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
        swipeTriggered(static_cast<QSwipeGesture *>(swipe));
    return true;
}

void MainWindow::swipeTriggered(QSwipeGesture *gesture){
    if (gesture->state() == Qt::GestureFinished){
        if (gesture->verticalDirection() == QSwipeGesture::Up)
            goButtomLabel();
        if (gesture->verticalDirection() == QSwipeGesture::Down)
            goUpperLabel();
        update();
    }
}

void MainWindow::goButtomLabel(){
    // does something here
}

void MainWindow::goUpperLabel(){
   // does something here
}

有什么不对吗?我忘了什么吗?

我是Qt的新手,也是C ++的初学者,所以我非常感谢你的帮助。

非常感谢你!

0 个答案:

没有答案
相关问题