文章目录
前言
轮播图是指在一个模块或者窗口中,通过鼠标点击或滑动后,可以看到多张图片。不论是爱奇艺的视频播放器,还是网易云的音乐播放器中,我们都可以看到在其首页有一个轮播图模块。本篇,我们将介绍如何实现鼠标滑动触发的轮播图。
一、实现效果
二、实现原理
1.轮播窗口组成
该窗口由五个QLabel类的标签控件和五个QPushButton类的按钮控件组成。
2.标签控件作用
利用标签来插入圆点图片,如下图所示。
3.按钮控件作用
利用按钮来插入轮播图片,可以通过按钮的点击信号触发槽函数,实现点击后的界面跳转。
4.总体实现目标
轮播窗口将实现以下效果:当我们把鼠标滑入到不同的圆点图片标签时,展示与标签绑定的图片按钮。
三、实现流程
1.实现步骤简述
(1)创建标签和按钮,插入图片并初始化。
(2)在每个标签控件上安装了事件滤波器。
(3)将事件滤波器的触发事件设置为鼠标滑入触发。
(4)为每个标签控件设置相应的触发动作,即鼠标滑入后图片按钮切换。
2.编码前准备工作
- 在编写代码前,我们需要先准备好标签和按钮对应的素材图片,如下图所示。
3.创建轮播窗口类
可以看到下面的代码,我们在类中定义了五个标签和五个按钮控件,以及事件过滤器函数。
4.初始化标签和按钮控件
下图为部分初始化代码,若需要查看完整源码,
请下拉到文章末尾
。
5.标签控件安装事件过滤器
重点注意:事件过滤器在轮播窗口的构造函数中安装,且必须
先初始化标签再安装
,否则无法触发。
6.设置触发条件和触发动作
下图为事件过滤函数部分代码,触发对象为某个标签,触发条件为鼠标滑入,触发动作为显示对应图片。
四、完整源码
1.main.cpp文件
#include <QApplication>
#include "slideshowwin.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
slideshowWin w;
w.show();
return a.exec();
}
2.slideShow.h文件
#ifndef SLIDESHOWWIN_H
#define SLIDESHOWWIN_H
#include <QWidget>
#include <QEvent>
#include <QLabel>
#include <QPushButton>
class slideshowWin : public QWidget
{
Q_OBJECT
public:
explicit slideshowWin(QWidget *parent = 0);
QLabel *point1,*point2,*point3,*point4,*point5;
QPushButton *picBtn1,*picBtn2,*picBtn3,*picBtn4,*picBtn5;
bool eventFilter(QObject *obj, QEvent *event);
signals:
public slots:
};
#endif // SLIDESHOWWIN_H
3.slideShow.cpp文件
#include <QPixmap>
#include <QDebug>
#include <QMouseEvent>
#include "slideshowwin.h"
slideshowWin::slideshowWin(QWidget *parent) : QWidget(parent)
{
//设置窗口大小
this->setFixedSize(995,295);
//创建按钮并初始化
this->picBtn1 = new QPushButton(this);
this->picBtn1->setGeometry(0,0,995,295);
this->picBtn1->setStyleSheet("QPushButton{border-image:url(./picture/LB1.png);}");
this->picBtn1->show();
this->picBtn2 = new QPushButton(this);
this->picBtn2->setGeometry(0,0,995,295);
this->picBtn2->setStyleSheet("QPushButton{border-image:url(./picture/LB2.png);}");
this->picBtn2->hide();
this->picBtn3 = new QPushButton(this);
this->picBtn3->setGeometry(0,0,995,295);
this->picBtn3->setStyleSheet("QPushButton{border-image:url(./picture/LB3.png);}");
this->picBtn3->hide();
this->picBtn4 = new QPushButton(this);
this->picBtn4->setGeometry(0,0,995,295);
this->picBtn4->setStyleSheet("QPushButton{border-image:url(./picture/LB4.png);}");
this->picBtn4->hide();
this->picBtn5 = new QPushButton(this);
this->picBtn5->setGeometry(0,0,995,295);
this->picBtn5->setStyleSheet("QPushButton{border-image:url(./picture/LB5.png);}");
this->picBtn5->hide();
//创建标签并初始化
this->point1 = new QLabel(this);
this->point1->setGeometry(420,250,24,30);
this->point1->setPixmap(QPixmap("./picture/c1.png"));
this->point1->setScaledContents(true);
this->point2 = new QLabel(this);
this->point2->setGeometry(460,250,24,30);
this->point2->setPixmap(QPixmap("./picture/c2.png"));
this->point2->setScaledContents(true);
this->point3 = new QLabel(this);
this->point3->setGeometry(500,250,24,30);
this->point3->setPixmap(QPixmap("./picture/c2.png"));
this->point3->setScaledContents(true);
this->point4 = new QLabel(this);
this->point4->setGeometry(540,250,24,30);
this->point4->setPixmap(QPixmap("./picture/c2.png"));
this->point4->setScaledContents(true);
this->point5 = new QLabel(this);
this->point5->setGeometry(580,250,24,30);
this->point5->setPixmap(QPixmap("./picture/c2.png"));
this->point5->setScaledContents(true);
//安装事件过滤器,控件作为窗口的事件过滤器
this->point1->installEventFilter(this);
this->point2->installEventFilter(this);
this->point3->installEventFilter(this);
this->point4->installEventFilter(this);
this->point5->installEventFilter(this);
}
bool slideshowWin::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::Enter)
{
if(obj == this->point1)
{
this->point1->setPixmap(QPixmap("./picture/c1.png"));
this->point2->setPixmap(QPixmap("./picture/c2.png"));
this->point3->setPixmap(QPixmap("./picture/c2.png"));
this->point4->setPixmap(QPixmap("./picture/c2.png"));
this->point5->setPixmap(QPixmap("./picture/c2.png"));
this->picBtn1->show();
this->picBtn2->hide();
this->picBtn3->hide();
this->picBtn4->hide();
this->picBtn5->hide();
return true;
}
else if(obj == this->point2)
{
this->point1->setPixmap(QPixmap("./picture/c2.png"));
this->point2->setPixmap(QPixmap("./picture/c1.png"));
this->point3->setPixmap(QPixmap("./picture/c2.png"));
this->point4->setPixmap(QPixmap("./picture/c2.png"));
this->point5->setPixmap(QPixmap("./picture/c2.png"));
this->picBtn1->hide();
this->picBtn2->show();
this->picBtn3->hide();
this->picBtn4->hide();
this->picBtn5->hide();
return true;
}
else if(obj == this->point3)
{
this->point1->setPixmap(QPixmap("./picture/c2.png"));
this->point2->setPixmap(QPixmap("./picture/c2.png"));
this->point3->setPixmap(QPixmap("./picture/c1.png"));
this->point4->setPixmap(QPixmap("./picture/c2.png"));
this->point5->setPixmap(QPixmap("./picture/c2.png"));
this->picBtn1->hide();
this->picBtn2->hide();
this->picBtn3->show();
this->picBtn4->hide();
this->picBtn5->hide();
return true;
}
else if(obj == this->point4)
{
this->point1->setPixmap(QPixmap("./picture/c2.png"));
this->point2->setPixmap(QPixmap("./picture/c2.png"));
this->point3->setPixmap(QPixmap("./picture/c2.png"));
this->point4->setPixmap(QPixmap("./picture/c1.png"));
this->point5->setPixmap(QPixmap("./picture/c2.png"));
this->picBtn1->hide();
this->picBtn2->hide();
this->picBtn3->hide();
this->picBtn4->show();
this->picBtn5->hide();
return true;
}
else if(obj == this->point5)
{
this->point1->setPixmap(QPixmap("./picture/c2.png"));
this->point2->setPixmap(QPixmap("./picture/c2.png"));
this->point3->setPixmap(QPixmap("./picture/c2.png"));
this->point4->setPixmap(QPixmap("./picture/c2.png"));
this->point5->setPixmap(QPixmap("./picture/c1.png"));
this->picBtn1->hide();
this->picBtn2->hide();
this->picBtn3->hide();
this->picBtn4->hide();
this->picBtn5->show();
return true;
}
return false;
}
return QWidget::eventFilter(obj, event);
}
总结
以上就是实现鼠标滑动触发图片轮播的所有内容,希望大家阅读后都能有所收获!需要图片素材及相应的工程源文件的读者,关注博主并在该篇文章下方评论写下邮箱地址
,将以压缩包形式发送。
原创不易,转载请标明出处,若文章出现有误之处,欢迎读者留言指正批评!