溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Qt如何實(shí)現(xiàn)櫻花飛舞效果

發(fā)布時(shí)間:2020-07-21 10:59:13 來(lái)源:億速云 閱讀:209 作者:小豬 欄目:開(kāi)發(fā)技術(shù)

小編這次要給大家分享的是Qt如何實(shí)現(xiàn)櫻花飛舞效果,文章內(nèi)容豐富,感興趣的小伙伴可以來(lái)了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

應(yīng)女友要求,使用Qt做了一個(gè)在電腦桌面櫻花飛舞的小程序。這里面用到了Qt動(dòng)畫(huà)效果QPropertyAnimation類(lèi)來(lái)控制飛舞效果。使用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)建櫻花動(dòng)畫(huà)
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); //無(wú)限循環(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);
 }
}

效果如下圖所示:

Qt如何實(shí)現(xiàn)櫻花飛舞效果

看完這篇關(guān)于Qt如何實(shí)現(xiàn)櫻花飛舞效果的文章,如果覺(jué)得文章內(nèi)容寫(xiě)得不錯(cuò)的話,可以把它分享出去給更多人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI