Qt实现左右滑动切换图片(可在开发板上滑动切换)

想用Qt在开发板上实现滑动切换图片,以前用过C语言写过,不过现在要用Qt实现,查阅了相关网页博客,发现大多数都是点击实现图片切换或自动切换,但这不太符合开发板的应用场景,于是就记录一下滑动切换图片的开发过程。

主要还是使用eventFilter()点击事件,获取点击和松开时的坐标,对其进行判断,然后设置相关逻辑切换图片

直接上源码

pictest.h

#ifndef PICTEST_H
#define PICTEST_H

#include <QDialog>

namespace Ui {
class Pictest;
}

class Pictest : public QDialog
{
    Q_OBJECT

public:
    explicit Pictest(QWidget *parent = 0);
    ~Pictest();

protected:
    bool eventFilter(QObject *watch, QEvent *evn);      //点击事件函数

private:
    Ui::Pictest *ui;
    int index = 2;      //图片标记值
};

#endif // PICTEST_H

 pictest.cpp

#include "pictest.h"
#include "ui_pictest.h"
#include "qevent.h"
#include <QDebug>

Pictest::Pictest(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Pictest)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);    //出去窗体边框
    this->setStyleSheet(QString("background-image: url(:/%1.png);").arg(2));    //显示一张图片
    this->installEventFilter(this);     //设置点击事件
}

Pictest::~Pictest()
{
    delete ui;
}

//点击事件函数
bool Pictest::eventFilter(QObject *watch, QEvent *evn)
{

    static int press_x;     //点击鼠标时获取的横坐标x
    static int press_y;     //点击鼠标时获取的纵坐标y
    static int relea_x;     //松开鼠标时获取的横坐标x
    static int relea_y;     //松开鼠标时获取的纵坐标y

    QMouseEvent *event = static_cast<QMouseEvent *>(evn);       //将图片QT QEvent 转换为 QMouseEvent ,QKeyEvent....等子类
    //获取点击鼠标(手指)时的坐标
    if (event->type() == QEvent::MouseButtonPress)
    {
            press_x = event->globalX();
            press_y = event->globalY();
    }
    //获取松开鼠标(手指)时的坐标
    if(event->type() == QEvent::MouseButtonRelease)
    {
        relea_x = event->globalX();
        relea_y = event->globalY();
    }
    //对鼠标(手指)滑动的方向进行判断(右滑)
    if((relea_x - press_x) > 5 && event->type() == QEvent::MouseButtonRelease && qAbs(relea_y - press_y) < 50)
    {
        if (index == 5) {
            index = 2;
        } else {
            index++;
        }
        this->setStyleSheet(QString("background-image: url(:/%1.png);").arg(index));        //切换图片
    }
    //对鼠标(手指)滑动的方向进行判断(左滑)
    if((press_x - relea_x) > 5 && event->type() == QEvent::MouseButtonRelease && qAbs(relea_y - press_y) < 50)
    {
        if(index == 2)
        {
            index = 5;
        }
        else
        {
            index--;
        }
        this->setStyleSheet(QString("background-image: url(:/%1.png);").arg(index));        //切换图片
    }

    return QWidget::eventFilter(watch, evn);
}

资源文件为2.png,3.png,4.png,5.png四张图片。

运行后在电脑上使用鼠标左右滑动图片可以进行切换,在开发板上用手指左右滑动图片可以进行切换。

源码下载地址,和博客所见一样:Qt实现滑动切换图片源码

  • 5
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ONESTAR博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值