手势、鼠标滑动实现界面切换

手势:

cpp 复制代码
#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
}
cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>
#include <QGestureEvent>
#include <QPanGesture>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    bool event(QEvent *event) override;
    bool gestureEvent(QGestureEvent *event);

private:
    QStackedWidget *stackedWidget;
    void setupUI();
};

#endif // MAINWINDOW_H
cpp 复制代码
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QWidget>
#include <QGesture>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setupUI();
    grabGesture(Qt::PanGesture);
}

MainWindow::~MainWindow()
{
}

void MainWindow::setupUI()
{
    // 创建中央部件
    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    // 创建布局
    QVBoxLayout *layout = new QVBoxLayout(centralWidget);

    // 创建堆叠部件
    stackedWidget = new QStackedWidget(this);
    layout->addWidget(stackedWidget);

    // 创建三个示例页面
    QWidget *page1 = new QWidget();
    QWidget *page2 = new QWidget();
    QWidget *page3 = new QWidget();

    // 为每个页面添加标签
    QVBoxLayout *layout1 = new QVBoxLayout(page1);
    QLabel *label1 = new QLabel("Page 1", page1);
    label1->setAlignment(Qt::AlignCenter);
    layout1->addWidget(label1);

    QVBoxLayout *layout2 = new QVBoxLayout(page2);
    QLabel *label2 = new QLabel("Page 2", page2);
    label2->setAlignment(Qt::AlignCenter);
    layout2->addWidget(label2);

    QVBoxLayout *layout3 = new QVBoxLayout(page3);
    QLabel *label3 = new QLabel("Page 3", page3);
    label3->setAlignment(Qt::AlignCenter);
    layout3->addWidget(label3);

    // 将页面添加到堆叠部件
    stackedWidget->addWidget(page1);
    stackedWidget->addWidget(page2);
    stackedWidget->addWidget(page3);

    // 设置窗口大小
    resize(400, 300);
}

bool MainWindow::event(QEvent *event)
{
    if (event->type() == QEvent::Gesture)
        return gestureEvent(static_cast<QGestureEvent*>(event));
    return QMainWindow::event(event);
}

bool MainWindow::gestureEvent(QGestureEvent *event)
{
    if (QGesture *pan = event->gesture(Qt::PanGesture)) {
        QPanGesture *panGesture = static_cast<QPanGesture*>(pan);
        QPointF delta = panGesture->delta();

        // 检测水平滑动
        if (qAbs(delta.x()) > qAbs(delta.y())) {
            int currentIndex = stackedWidget->currentIndex();
            int count = stackedWidget->count();

            if (delta.x() > 0 && currentIndex > 0) {
                // 向右滑动,显示上一页
                stackedWidget->setCurrentIndex(currentIndex - 1);
            } else if (delta.x() < 0 && currentIndex < count - 1) {
                // 向左滑动,显示下一页
                stackedWidget->setCurrentIndex(currentIndex + 1);
            }
            return true;
        }
    }
    return false;
}

鼠标:

cpp 复制代码
#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
}
cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>
#include <QPoint>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;

private:
    QStackedWidget *stackedWidget;
    QPoint lastMousePos;
    bool isDragging;
    int dragThreshold;  // 拖动阈值,超过这个值才触发页面切换
    void setupUI();
};

#endif // MAINWINDOW_H
cpp 复制代码
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QWidget>
#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , isDragging(false)
    , dragThreshold(50)  // 设置拖动阈值为50像素
{
    setupUI();
}

MainWindow::~MainWindow()
{
}

void MainWindow::setupUI()
{
    // 创建中央部件
    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    // 创建布局
    QVBoxLayout *layout = new QVBoxLayout(centralWidget);

    // 创建堆叠部件
    stackedWidget = new QStackedWidget(this);
    layout->addWidget(stackedWidget);

    // 创建三个示例页面
    QWidget *page1 = new QWidget();
    QWidget *page2 = new QWidget();
    QWidget *page3 = new QWidget();

    // 为每个页面添加标签和背景色
    QVBoxLayout *layout1 = new QVBoxLayout(page1);
    QLabel *label1 = new QLabel("Page 1", page1);
    label1->setAlignment(Qt::AlignCenter);
    label1->setStyleSheet("QLabel { font-size: 24px; }");
    layout1->addWidget(label1);
    page1->setStyleSheet("background-color: #FFE4E1;");  // 浅粉色背景

    QVBoxLayout *layout2 = new QVBoxLayout(page2);
    QLabel *label2 = new QLabel("Page 2", page2);
    label2->setAlignment(Qt::AlignCenter);
    label2->setStyleSheet("QLabel { font-size: 24px; }");
    layout2->addWidget(label2);
    page2->setStyleSheet("background-color: #E0FFFF;");  // 浅青色背景

    QVBoxLayout *layout3 = new QVBoxLayout(page3);
    QLabel *label3 = new QLabel("Page 3", page3);
    label3->setAlignment(Qt::AlignCenter);
    label3->setStyleSheet("QLabel { font-size: 24px; }");
    layout3->addWidget(label3);
    page3->setStyleSheet("background-color: #F0FFF0;");  // 浅绿色背景

    // 将页面添加到堆叠部件
    stackedWidget->addWidget(page1);
    stackedWidget->addWidget(page2);
    stackedWidget->addWidget(page3);

    // 设置窗口大小
    resize(400, 300);
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        lastMousePos = event->pos();
        isDragging = true;
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (!isDragging) return;

    QPoint currentPos = event->pos();
    int deltaX = currentPos.x() - lastMousePos.x();

    // 如果水平移动距离超过阈值,则移动当前页面
    if (qAbs(deltaX) > dragThreshold) {
        int currentIndex = stackedWidget->currentIndex();
        int count = stackedWidget->count();

        if (deltaX > 0 && currentIndex > 0) {
            // 向右拖动,显示上一页
            stackedWidget->setCurrentIndex(currentIndex - 1);
            isDragging = false;
        } else if (deltaX < 0 && currentIndex < count - 1) {
            // 向左拖动,显示下一页
            stackedWidget->setCurrentIndex(currentIndex + 1);
            isDragging = false;
        }
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    isDragging = false;
}
相关推荐
小学生的信奥之路5 分钟前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
曙曙学编程1 小时前
stm32——GPIO
c语言·c++·stm32·单片机·嵌入式硬件
△曉風殘月〆1 小时前
Visual Studio中的常用调试功能(下)
c++·ide·visual studio·调试
武当豆豆1 小时前
C++编程学习(第25天)
开发语言·c++·学习
minji...5 小时前
C++ string类(STL简介 , string类 , 访问修改字符)
开发语言·c++
Forward♞5 小时前
Qt——文件操作
开发语言·c++·qt
十五年专注C++开发5 小时前
CMake进阶: CMake Modules---简化CMake配置的利器
linux·c++·windows·cmake·自动化构建
winds~6 小时前
【git】 撤销revert一次commit中的某几个文件
linux·c++
carver w7 小时前
MFC,C++,海康SDK,回调,轮询
开发语言·c++·mfc
王廷胡_白嫖帝7 小时前
Qt猜数字游戏项目开发教程 - 从零开始构建趣味小游戏
开发语言·qt·游戏