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

在学习Qt的这2、3个月里,对Qt越发感兴趣,从刚开始的盲目、无所适从到现在的学习、研究、熟练、掌握的过程中,我学到了很多东西,也学会了如何通过自学让自己更加成熟、强大起来,如何更有效地提高自己学习、工作效率。

关于Qt界面的出现消失效果,我简单介绍两种方法。

1、

(1)界面出现

将下面这段代码放在界面的构造函数当中就行

//界面动画,改变透明度的方式出现0 - 1渐变
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
animation->setDuration(1000);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start();

(2)界面消失:

既然是界面消失,应当是按下关闭按钮时界面消失,如下:

//连接关闭按钮信号和槽

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、

在这里贴出消失时候的代码,其实出现的时候类似

界面消失:

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、付费专栏及课程。

余额充值