首页 分享 基于Qt实现桌面宠物

基于Qt实现桌面宠物

来源:萌宠菠菠乐园 时间:2024-10-13 02:19

最新推荐文章于 2024-07-11 17:20:26 发布

低年级学生 于 2022-10-18 23:12:55 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

a总体效果

主要功能 

可以通过设置进行桌面壁纸的切换;点击天气按钮可查看实时天气;显示实时时间

部分代码

widget.h

#pragma once

#include <QWidget>

#include <QPixmap>

#include<qlabel.h>

#include<QPushButton>

#include"desktopwidget.h"

class Widget : public QWidget

{

Q_OBJECT

public:

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_OBJECT

public:

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);

});

}

关注私信博主可获得全部资源     

         

相关知识

用QT实现一个简单的桌面宠物
基于Qt实现桌面宠物
拓海/基于Qt的桌面宠物
基于C#制作一个桌面宠物
QQ宠物桌面是怎么开发的
用c语言写一个桌面宠物
Python实现桌面挂件,做一只可爱的桌面宠物~
Qt
基于springboot+mybatis+mysql+html实现宠物医院管理系统(包含实验报告)资源
Python实现可以语音聊天的桌面宠物程序

网址: 基于Qt实现桌面宠物 https://www.mcbbbk.com/newsview371667.html

所属分类:萌宠日常
上一篇: 基于C#制作一个桌面宠物
下一篇: 【菜菜丸的菜鸟教程】桌宠制作经验

推荐分享