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();
}

 

    對比看來,第二種方法比較笨拙,而且效率差,所以優先選擇方法一,其實學習就是一個累積的過程,沒有對比就沒有進步,只要是可以行通的,不妨多下點功夫研究一下,條條大路通羅馬,知識在與鑽研、分享!

http://blog.sina.com.cn/s/blog_a6fb6cc90101awhm.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM