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

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

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(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–;

}

你可能感兴趣的:(qt,命令模式,开发语言)