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

手势:

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;
}
相关推荐
凡人叶枫6 分钟前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
CSDN_RTKLIB7 分钟前
使用三方库头文件未使用导出符号情景
c++
rainbow68891 小时前
Linux文件描述符与重定向原理
c++
mengzhi啊2 小时前
QUndoView 本质是一个 Qt 界面控件(继承自 QListView),专门适配 QUndoStack
qt
CodeSheep程序羊2 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
编程小白20263 小时前
从 C++ 基础到效率翻倍:Qt 开发环境搭建与Windows 神级快捷键指南
开发语言·c++·windows·qt·学习
深蓝海拓3 小时前
PySide6,QCoreApplication::aboutToQuit与QtQore.qAddPostRoutine:退出前后的清理工作
笔记·python·qt·学习·pyqt
薛定谔的猫喵喵3 小时前
天然气压力能利用系统综合性评价平台:基于Python和PyQt5的AHP与模糊综合评价集成应用
开发语言·python·qt
.小墨迹3 小时前
apollo学习之借道超车的速度规划
linux·c++·学习·算法·ubuntu
云中飞鸿3 小时前
linux中qt安装
开发语言·qt