Qt實(shí)現(xiàn)櫻花飛舞效果
本文實(shí)例為大家分享了Qt實(shí)現(xiàn)櫻花飛舞效果的具體代碼,供大家參考,具體內(nèi)容如下
應(yīng)女友要求,使用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()); //去除窗體標(biāo)題 this->resize(qApp->desktop()->availableGeometry().size()); this->setAttribute(Qt::WA_TranslucentBackground, true); //設(shè)置背景透明 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); } //批量創(chuàng)建櫻花標(biāo)簽 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); } } //批量創(chuàng)建櫻花動畫 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); //無限循環(huán) ani->setStartValue(QRect(rnds[i*2],0,200,60)); ani->setEndValue(QRect(rnds[2*i+1],this->height()-50,200,60)); animations.append(ani); } }
效果如下圖所示:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言實(shí)現(xiàn)Fibonacci數(shù)列遞歸
這篇文章主要介紹了C語言實(shí)現(xiàn)Fibonacci數(shù)列遞歸,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02解析C++中的虛擬函數(shù)及其靜態(tài)類型和動態(tài)類型
虛擬函數(shù)(Visual Function)亦常被成為虛函數(shù),是C++中的一個重要特性,本文我們就來解析C++中的虛擬函數(shù)及其靜態(tài)類型和動態(tài)類型2016-06-06求素數(shù),用vector存儲的實(shí)現(xiàn)方法
本篇文章是對求素數(shù),用vector存儲的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C語言學(xué)習(xí)之鏈表的實(shí)現(xiàn)詳解
鏈表是一種物理存儲結(jié)構(gòu)上非連續(xù)、非順序的存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。這篇文章主要介紹了C語言中鏈表的實(shí)現(xiàn),需要的可以參考一下2022-11-11C標(biāo)準(zhǔn)庫<assert.h>的實(shí)現(xiàn)詳解
這篇文章主要介紹了C標(biāo)準(zhǔn)庫<assert.h>的實(shí)現(xiàn),主要包括了<assert.h>的基本概念、實(shí)現(xiàn)及用法等,需要的朋友可以參考下2014-09-09