a总体效果
主要功能
- 可以通过设置进行桌面壁纸的切换;
- 点击天气按钮可查看实时天气;
- 显示实时时间
部分代码
widget.h
#pragma once#include <QWidget>
#include <QPixmap>
#include<qlabel.h>
#include<QPushButton>
#include"desktopwidget.h"
class Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void updateRoleAnimation();bool eventFilter(QObject* watched, QEvent* ev) override;void initBtn();
private:QLabel* roleLabel;qint8 curFrame; //当前帧QPushButton* closeBtn;QPushButton* cutBtn;QPushButton* openBtn;DesktopWidget* desktopWidget;};
desktopwidget.h
#pragma once#include <QWidget>
#include<QLabel>
#include<QPixmap>
class DesktopWidget : public QWidget
{Q_OBJECTpublic:DesktopWidget(QWidget *parent = nullptr);~DesktopWidget();void setAllWallpaper();void setPixmap(const QString& fileNname);
private:QLabel* bkLabel; //放壁纸QPixmap bkPixmap;
};
widget.cpp
#include "widget.h"
#include<QTimer>
#include<QGraphicsDropShadowEffect>
#include<QMouseEvent>
#include<QFileDialog>
Widget::Widget(QWidget *parent): QWidget(parent), roleLabel(new QLabel(this)),curFrame(0), desktopWidget(new DesktopWidget)
{//去掉窗口的边框,和让背景透明setWindowFlags(Qt::WindowType::FramelessWindowHint);setAttribute(Qt::WA_TranslucentBackground);//使用定时器去更新动画QTimer* updateTimer = new QTimer(this);updateTimer->callOnTimeout(this, &Widget::updateRoleAnimation);updateTimer->start(500);//给窗口设置阴影QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(this);effect->setColor(QColor(230, 231, 232,220));effect->setBlurRadius(5);this->setGraphicsEffect(effect);this->installEventFilter(this);roleLabel->resize(500, 500);desktopWidget->show();initBtn();
}Widget::~Widget()
{}void Widget::updateRoleAnimation()
{QString qss("background-repeat:no-repeat;");roleLabel->setStyleSheet(qss+QString("background-image:url(:/resource/desktopRole/summerGril/%1.png);").arg(curFrame));curFrame = (curFrame + 1) % 6;
}bool Widget::eventFilter(QObject* watched, QEvent* ev)
{QMouseEvent* mouseev = static_cast<QMouseEvent*>(ev);//判断鼠标左键按下static QPoint begpos;if (ev->type() == QEvent::MouseButtonPress){begpos = mouseev->globalPos() - this->pos();}//判断鼠标移动else if (ev->type() == QEvent::MouseMove &&mouseev->buttons() & Qt::MouseButton::LeftButton){this->move(mouseev->globalPos() - begpos);}return false;
}void Widget::initBtn()
{closeBtn = new QPushButton(this);cutBtn = new QPushButton(this);openBtn = new QPushButton(this);closeBtn->setGeometry(300, 200, 32, 32);cutBtn->setGeometry(300, 240, 32, 32);openBtn->setGeometry(300, 280, 32, 32);closeBtn->setObjectName("closeBtn");closeBtn->setStyleSheet("background-image:url(:/resource/button/quit.png);}");cutBtn->setStyleSheet("background-image:url(:/resource/button/cut.png);");openBtn->setStyleSheet("background-image:url(:/resource/button/open.png);");this->setStyleSheet("QPushButton{background-color:rgb(64,173,250);\border:none;border-radius:5px;}\
QPushButton#closeBtn:hover{background-color:rgb(233,31,48);}");connect(closeBtn, &QPushButton::pressed, this, &Widget::close);connect(openBtn, &QPushButton::pressed, this, [=](){QString filename = QFileDialog::getOpenFileName(nullptr, "选择壁纸", "./", "Image (*.jpg *.png)");if (filename.isEmpty()){return;}desktopWidget->setPixmap(filename);});
}
关注私信博主可获得全部资源