温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Qt如何实现樱花飞舞效果

发布时间:2020-07-21 10:59:13 来源:亿速云 阅读:190 作者:小猪 栏目:开发技术

小编这次要给大家分享的是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);
 }
}

效果如下图所示:

Qt如何实现樱花飞舞效果

看完这篇关于Qt如何实现樱花飞舞效果的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI