首页 > 分享 > Qt二次元桌面宠物

Qt二次元桌面宠物

项目背景

该桌面宠物项目是本人在学习Qt后不久,在看了众多视频和许多博客后,自己尝试写的一个桌面项目,由于本人学识浅薄,项目中难免有许多bug和不足之处,请多包涵,同时在本文末尾,笔者也提供了该项目的源码。

技术要求 

1、控件 --按钮

2、信号与槽机制

3、绘图

4、定时器

5、鼠标事件

是不是很简单,本项目主要还是提供一下大致思路,有了这些思路后,大家也可以写出自己的桌面宠物。

效果展示 

按钮1 (从上至下)

退出功能 

按钮2  动画

 按钮3  动画

按钮4 移动

 

鼠标移至桌面宠物窗口上,移动就会停止,同时也可进行拖拽 

代码:

widget.h

#ifndef WIDGET_H

#define WIDGET_H

#include <QWidget>

#include "QPixmap"

#include "QPainter"

#include "QPaintEvent"

#include "QMouseEvent"

#include "QPushButton"

#include "QDesktopWidget"

#include "QMediaPlayer"

QT_BEGIN_NAMESPACE

namespace Ui { class Widget; }

QT_END_NAMESPACE

class Widget : public QWidget

{

Q_OBJECT

public:

Widget(QWidget *parent = nullptr);

~Widget();

void mouseMoveEvent(QMouseEvent *);

void mousePressEvent(QMouseEvent *);

void enterEvent(QEvent *);

private:

Ui::Widget *ui;

QPixmap pix;

void paintEvent(QPaintEvent *);

int pic_max = 3;

int pic_min = 1;

QTimer *timer;

QPoint m_pos;

QPushButton *close;

QPushButton *movie_1;

QPushButton *movie_2;

QPushButton *movie_3;

QString pic_adrress;

QString pic_adrress_1 = ":/img/shime11";

QString pic_adrress_2 = ":/img/shime22";

QString pic_adrress_3 = ":/img/shime33";

QString pic_adrress_4 = ":/img/shime44";

QPoint current_pos;

QDesktopWidget *desk;

QTimer *timer_move;

QString music_ads;

QString music_ads_1 = ":/sound/ha_qian.wav";

QString music_ads_2 = ":/sound/run.wav";

public slots:

void showMovie();

void showMovie1();

void showMovie2();

void showMovie3();

void showMovie3Move();

};

#endif

 widget.cpp

#include "widget.h"

#include "ui_widget.h"

#include "QTimer"

#include "QDebug"

#include "QPoint"

#include"QSound"

Widget::Widget(QWidget *parent)

: QWidget(parent)

, ui(new Ui::Widget)

{

ui->setupUi(this);

pix.load(":/img/shime111.png");

this->setWindowFlags(Qt::WindowType::FramelessWindowHint);

this->setAttribute(Qt::WA_TranslucentBackground);

this->setFixedSize(pix.width(),pix.height());

this->setWindowFlags(this->windowFlags() |Qt::Tool);

this->desk = QApplication::desktop();

this->current_pos = QPoint(desk->width()-this->width(),desk->height()/2);

this->move(current_pos);

this->pic_adrress = this->pic_adrress_1;

timer = new QTimer(this);

connect(timer,&QTimer::timeout,this,&Widget::showMovie);

timer->start(500);

this->close = new QPushButton(this);

this->close->move(210,0);

this->close->setIcon(QIcon(":/other/btn1.png"));

connect(this->close,&QPushButton::clicked,[=](){

exit(0);

});

this->movie_1 = new QPushButton(this);

this->movie_1->move(210,25);

this->movie_1->setIcon(QIcon(":/other/btn1.png"));

connect(this->movie_1,&QPushButton::clicked,this,&Widget::showMovie1);

this->movie_2 = new QPushButton(this);

this->movie_2->move(210,50);

this->movie_2->setIcon(QIcon(":/other/btn1.png"));

connect(this->movie_2,&QPushButton::clicked,this,&Widget::showMovie2);

this->movie_3 = new QPushButton(this);

this->movie_3->move(210,75);

this->movie_3->setIcon(QIcon(":/other/btn1.png"));

connect(this->movie_3,&QPushButton::clicked,this,&Widget::showMovie3);

timer_move = new QTimer(this);

connect(timer_move,&QTimer::timeout,this,&Widget::showMovie3Move);

}

void Widget::paintEvent(QPaintEvent *)

{

QPainter painter(this);

painter.drawPixmap(0,0,pix);

}

void Widget::showMovie()

{

if(pic_min == 1){

if(this->music_ads.length()!=0)

{

QSound::play(this->music_ads);

}

}

QString str = pic_adrress+QString("%1").arg(pic_min++);

if(pic_min>pic_max){

pic_min = 1;

}

pix.load(str);

update();

}

void Widget::mouseMoveEvent(QMouseEvent *e)

{

this->current_pos = QPoint(e->globalPos()-m_pos);

this->move(e->globalPos()-this->m_pos);

}

void Widget::mousePressEvent(QMouseEvent *e)

{

m_pos = e->globalPos()-this->pos();

}

void Widget::showMovie1()

{

this->music_ads = this->music_ads_1;

this->pic_adrress = this->pic_adrress_2;

this->pic_min = 1;

this->pic_max = 5;

}

void Widget::showMovie2()

{

this->music_ads = "";

this->pic_adrress = this->pic_adrress_3;

this->pic_min = 1;

this->pic_max = 7;

}

void Widget::showMovie3()

{

this->pic_adrress = this->pic_adrress_4;

this->pic_min = 1;

this->pic_max = 2;

timer_move->start(30);

}

void Widget::showMovie3Move()

{

this->music_ads = this->music_ads_2;

this->current_pos.setX(current_pos.x()-5);

if(current_pos.x()<(-this->width())){

this->current_pos.setX(desk->width());

}

this->move(current_pos);

}

void Widget::enterEvent(QEvent *)

{

timer_move->stop();

}

Widget::~Widget()

{

delete ui;

}

源码

链接:https://pan.baidu.com/s/1ccjEbdLH2h1dIQ6RzxVR1Q 
提取码:1314

相关知识

用QT实现一个简单的桌面宠物
【QT项目实战】自制桌面宠物!当我学了qt窗口开发之后,就把原神的纳西妲做成了桌面宠物!
QT桌面宠物+桌面大鹅(1)
【C/C++技术教学】Qt自制桌面宠物!思路分析+代码演示丨程序员教你如何把喜欢的角色做成随便把玩的桌面宠物!
基于Qt实现桌面宠物
喵萌桌面宠物app下载 喵萌桌面宠物(二次元手机桌宠软件) v6.6.1 安卓手机版 下载
Qt
QQ宠物桌面是怎么开发的
桌面宠物app哪个好?手机桌面宠物软件
用c语言写一个桌面宠物

网址: Qt二次元桌面宠物 https://m.mcbbbk.com/newsview726028.html

所属分类:萌宠日常
上一篇: 宠物免费领养,你真的了解其中的真
下一篇: 电脑桌宠软件都有哪些