Qt之界面出现、消失动画效果

1、
(1)界面出现
将下面这段代码放在界面的构造函数当中就行
[C++]  纯文本查看 复制代码
?
1
2
3
4
5
6
//界面动画,改变透明度的方式出现0 - 1渐变
QPropertyAnimation *animation = new QPropertyAnimation( this , "windowOpacity" );
animation->setDuration(1000);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start();
(2)界面消失:
既然是界面消失,应当是按下关闭按钮时界面消失,如下:
[C++]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
//连接关闭按钮信号和槽
QObject::connect(close_button, SIGNAL(clicked()), this , SLOT(closeWidget()));
  
//槽函数如下,主要进行界面透明度的改变,完成之后连接槽close来调用closeEvent事件
bool LoginDialog::closeWidget()
{
    //界面动画,改变透明度的方式消失1 - 0渐变
    QPropertyAnimation *animation = new QPropertyAnimation( this , "windowOpacity" );
    animation->setDuration(1000);
    animation->setStartValue(1);
    animation->setEndValue(0);
    animation->start();
    connect(animation, SIGNAL(finished()), this , SLOT(close()));
  
    return true
}
void LoginDialog::closeEvent(QCloseEvent *)
{
     //退出系统
     QApplication::quit();
}
2、在这里贴出消失时候的代码,其实出现的时候类似

界面消失:
[C++]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void LoginDialog::closeEvent(QCloseEvent *)
{
  for ( int i=0; i< 100000; i++)
  {
   if (i<10000)
   {
    this ->setWindowOpacity(0.9);
   }
   else if (i<20000)
   {
    this ->setWindowOpacity(0.8);
   }
   else if (i<30000)
   {
    this ->setWindowOpacity(0.7);
   }
   else if (i<40000)
   {
    this ->setWindowOpacity(0.6);
   }
   else if (i<50000)
   {
    this ->setWindowOpacity(0.5);
   }
   else if (i<60000)
   {
    this ->setWindowOpacity(0.4);
   }
   else if (i<70000)
   {
    this ->setWindowOpacity(0.3);
   }
   else if (i<80000)
   {
    this ->setWindowOpacity(0.2);
   }
   else if (i<90000)
   {
    this ->setWindowOpacity(0.1);
   }
   else
   {
    this ->setWindowOpacity(0.0);
   }
  }
 
  //进行窗口退出
   QApplication::quit();
}
    对比看来,第二种方法比较笨拙,而且效率差,所以优先选择方法一,其实学习就是一个累积的过程,没有对比就没有进步,只要是可以行通的,不妨多下点功夫研究一下,条条大路通罗马,知识在与钻研、分享!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值