如何用Qt编程实现一个长尾词的樱花飞舞动画效果?
- 内容介绍
- 文章标签
- 相关推荐
本文共计687个文字,预计阅读时间需要3分钟。
本文分享了Qt实现樱花飞舞效果的代码示例,供大家参考。具体内容包括:
1. 樱花飞舞效果的整体代码;
2.女友请求,使用Qt在电脑桌面上制作一个樱花飞舞的小程序;
3.采用了Qt动画效果QPropertyAnimation。
具体代码如下:
cpp
#include #include #include #include #include #include #includeint main(int argc, char *argv[]){ QApplication a(argc, argv);
// 创建场景和视图 QGraphicsScene scene; QGraphicsView view(&scene);
// 加载樱花图片 QPixmap pixmap(:/images/sakura.png);
// 创建樱花项 QGraphicsPixmapItem *item=new QGraphicsPixmapItem(pixmap);
// 设置樱花项的位置 item->setPos(400, 300);
// 创建动画 QPropertyAnimation *animation=new QPropertyAnimation(item, pos);
// 设置动画的起始位置和结束位置 animation->setStartValue(QPointF(400, 300)); animation->setEndValue(QPointF(200, 50));
// 设置动画的持续时间 animation->setDuration(3000);
// 启动动画 animation->start();
// 将樱花项添加到场景中 scene.addItem(item);
// 显示视图 view.show();
return a.exec();}
注意:在实际使用中,需要将`:/images/sakura.png`替换为樱花图片的实际路径。
本文实例为大家分享了Qt实现樱花飞舞效果的具体代码,供大家参考,具体内容如下
应女友要求,使用Qt做了一个在电脑桌面樱花飞舞的小程序。这里面用到了Qt动画效果QPropertyAnimation类来控制飞舞效果。使用label加载樱花图案。大概的核心代码如下:
Widget::Widget(QWidget *parent) : QWidget(parent), timer(new QTimer(this)), pixmap(new QPixmap(":/cherry.png")), ui(new Ui::Widget) { ui->setupUi(this); setWindowFlags(Qt::FramelessWindowHint | windowFlags()); //去除窗体标题 this->resize(qApp->desktop()->availableGeometry().size()); this->setAttribute(Qt::WA_TranslucentBackground, true); //设置背景透明 this->setAutoFillBackground(true); this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint); //窗口总在最顶层 connect(timer,SIGNAL(timeout()),this,SLOT(start())); QPixmap *pixmap = new QPixmap(":/cherry.png"); pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio); pixmaps.append(pixmap); pixmap = new QPixmap(":/cherry2.png"); pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio); pixmaps.append(pixmap); pixmap = new QPixmap(":/cherry3.png"); pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio); pixmaps.append(pixmap); pixmap = new QPixmap(":/cherry4.png"); pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio); pixmaps.append(pixmap); pixmap = new QPixmap(":/cherry5.png"); pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio); pixmaps.append(pixmap); creatLabels(); createAnimation(); timer->start(1000); } //批量创建樱花标签 void Widget::creatLabels() { for(int i = 0; i < cherryNums;i++) { QLabel *label = new QLabel(this); label->setScaledContents(true); label->setPixmap(*pixmaps[i%pixmaps.size()]); label->setAttribute(Qt::WA_TranslucentBackground, true); label->resize(0,0); labs.append(label); } } //批量创建樱花动画 void Widget::createAnimation() { if(labs.empty()) return; QVector<int> rnds = generateRandomNumber(labs.size()*2); for(int i = 0;i < labs.size();i++) { QPropertyAnimation *ani = new QPropertyAnimation(this); ani->setTargetObject(labs[i]); ani->setPropertyName("geometry"); ani->setDuration(10000); ani->setLoopCount(-1); //无限循环 ani->setStartValue(QRect(rnds[i*2],0,200,60)); ani->setEndValue(QRect(rnds[2*i+1],this->height()-50,200,60)); animations.append(ani); } }
效果如下图所示:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计687个文字,预计阅读时间需要3分钟。
本文分享了Qt实现樱花飞舞效果的代码示例,供大家参考。具体内容包括:
1. 樱花飞舞效果的整体代码;
2.女友请求,使用Qt在电脑桌面上制作一个樱花飞舞的小程序;
3.采用了Qt动画效果QPropertyAnimation。
具体代码如下:
cpp
#include #include #include #include #include #include #includeint main(int argc, char *argv[]){ QApplication a(argc, argv);
// 创建场景和视图 QGraphicsScene scene; QGraphicsView view(&scene);
// 加载樱花图片 QPixmap pixmap(:/images/sakura.png);
// 创建樱花项 QGraphicsPixmapItem *item=new QGraphicsPixmapItem(pixmap);
// 设置樱花项的位置 item->setPos(400, 300);
// 创建动画 QPropertyAnimation *animation=new QPropertyAnimation(item, pos);
// 设置动画的起始位置和结束位置 animation->setStartValue(QPointF(400, 300)); animation->setEndValue(QPointF(200, 50));
// 设置动画的持续时间 animation->setDuration(3000);
// 启动动画 animation->start();
// 将樱花项添加到场景中 scene.addItem(item);
// 显示视图 view.show();
return a.exec();}
注意:在实际使用中,需要将`:/images/sakura.png`替换为樱花图片的实际路径。
本文实例为大家分享了Qt实现樱花飞舞效果的具体代码,供大家参考,具体内容如下
应女友要求,使用Qt做了一个在电脑桌面樱花飞舞的小程序。这里面用到了Qt动画效果QPropertyAnimation类来控制飞舞效果。使用label加载樱花图案。大概的核心代码如下:
Widget::Widget(QWidget *parent) : QWidget(parent), timer(new QTimer(this)), pixmap(new QPixmap(":/cherry.png")), ui(new Ui::Widget) { ui->setupUi(this); setWindowFlags(Qt::FramelessWindowHint | windowFlags()); //去除窗体标题 this->resize(qApp->desktop()->availableGeometry().size()); this->setAttribute(Qt::WA_TranslucentBackground, true); //设置背景透明 this->setAutoFillBackground(true); this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint); //窗口总在最顶层 connect(timer,SIGNAL(timeout()),this,SLOT(start())); QPixmap *pixmap = new QPixmap(":/cherry.png"); pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio); pixmaps.append(pixmap); pixmap = new QPixmap(":/cherry2.png"); pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio); pixmaps.append(pixmap); pixmap = new QPixmap(":/cherry3.png"); pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio); pixmaps.append(pixmap); pixmap = new QPixmap(":/cherry4.png"); pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio); pixmaps.append(pixmap); pixmap = new QPixmap(":/cherry5.png"); pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio); pixmaps.append(pixmap); creatLabels(); createAnimation(); timer->start(1000); } //批量创建樱花标签 void Widget::creatLabels() { for(int i = 0; i < cherryNums;i++) { QLabel *label = new QLabel(this); label->setScaledContents(true); label->setPixmap(*pixmaps[i%pixmaps.size()]); label->setAttribute(Qt::WA_TranslucentBackground, true); label->resize(0,0); labs.append(label); } } //批量创建樱花动画 void Widget::createAnimation() { if(labs.empty()) return; QVector<int> rnds = generateRandomNumber(labs.size()*2); for(int i = 0;i < labs.size();i++) { QPropertyAnimation *ani = new QPropertyAnimation(this); ani->setTargetObject(labs[i]); ani->setPropertyName("geometry"); ani->setDuration(10000); ani->setLoopCount(-1); //无限循环 ani->setStartValue(QRect(rnds[i*2],0,200,60)); ani->setEndValue(QRect(rnds[2*i+1],this->height()-50,200,60)); animations.append(ani); } }
效果如下图所示:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

