目录
一.设置背景于初始化图像
二·.设置定时器
三.定时器到更新图片
四.鼠标点击暂停更新和打开更新
五.效果
六.代码
1.h
2.cpp
一.设置背景于初始化图像

二·.设置定时器

三.定时器到更新图片

四.鼠标点击暂停更新和打开更新

五.效果


六.代码
1.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    void setimage();
protected:
    bool eventFilter(QObject *watched, QEvent *evt);
private:
    Ui::Widget *ui;
    QTimer* m_pTimer;
    bool isUpdating;
};
#endif // WIDGET_H
2.cpp
#include "widget.h"
#include "ui_widget.h"
#include "qevent.h"
#include <QDebug>
#include <QTimer>
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);
    //透明背景:使用 setAttribute(Qt::WA_TranslucentBackground) 设置窗口为透明背景,这样可以创建更具视觉吸引力的效果。
    this->setAttribute(Qt::WA_TranslucentBackground);
    //无边框窗口:setWindowFlags() 方法将窗口设置为无边框(Qt::FramelessWindowHint),同时保留系统菜单和最小化按钮。
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
    //事件过滤器:调用 installEventFilter(this) 为 ui->widget 安装事件过滤器,以便处理鼠标事件。
    ui->widget->installEventFilter(this);
    //初始背景图像:通过样式表设置初始的背景图像。
    ui->widget->setStyleSheet(QString("background-image:url(:/image/%1.jfif);").arg(1));
    //定时器, 1s更新图片
    m_pTimer = new QTimer();
    m_pTimer->setInterval(1000);
    connect(m_pTimer, &QTimer::timeout, this, &Widget::setimage);
    m_pTimer->start();
    isUpdating = true;
}
Widget::~Widget()
{
    delete ui;
}
void Widget::setimage()
{
    static int index = 1;
    if(isUpdating){
        if (index == 12) {
            index = 1;
        } else {
            index++;
        }
    }
    ui->widget->setStyleSheet(QString("background-image:url(:/image/%1.jfif);").arg(index));
}
bool Widget::eventFilter(QObject *watched, QEvent *evt)
{
    if (watched == ui->widget && evt->type() == QEvent::MouseButtonPress) {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(evt);
        if (mouseEvent->button() == Qt::LeftButton) {
            isUpdating = !isUpdating; // 切换更新状态
            if (!isUpdating) {
                m_pTimer->stop(); // 停止定时器
            } else {
                m_pTimer->start(); // 恢复定时器
            }
            return true; // 表示事件已处理
        }
    }
    return QWidget::eventFilter(watched, evt); // 否则调用基类的事件过滤器
}



















