歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 給Qt程序添加啟動動畫

給Qt程序添加啟動動畫

日期:2017/3/1 11:10:59   编辑:Linux編程

一些應用程序啟動很慢時,一般會在啟動時顯示一個畫面,利用這種方法可以讓程序啟動時間不那麼長。給Qt應用程序加一個啟動畫面很簡單,需要使用的類是QSplashScreen,在窗口沒有顯示之前,QSplashScreen顯示一個圖片,他還可以在圖片上顯示文字信息提示用戶當前程序初始化的進度。一般情況下,啟動畫面代碼在main()函數中,加在調用QApplication::exec()之前

具體的實現如下:

  1. //main.cpp
  2. #include <QtGui/QApplication>
  3. #include <QTextCodec>
  4. #include <QSplashScreen>
  5. #include <QDesktopWidget>
  6. #include "mainwindow.h"
  7. int main(int argc, char *argv[])
  8. {
  9. //在程序中能使用中文必須進行設置字體處理
  10. QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
  11. QApplication a(argc, argv);
  12. QSplashScreen *splash = new QSplashScreen;
  13. splash->setPixmap(QPixmap("F:\\Windows QT\\Notepad\\start.jpg"));
  14. splash->show();
  15. //讓對話框延遲一段時間顯示
  16. for(int i=0;i<200;i++)
  17. {
  18. splash->repaint();
  19. }
  20. MainWindow w;
  21. w.show();
  22. //將窗口移動到屏幕的中央
  23. w.move ((QApplication::desktop()->width() - w.width())/2,(QApplication::desktop()->height() - w.height())/2);
  24. splash->finish(&w);
  25. delete splash;
  26. return a.exec();
  27. }

啟動畫面:

主程序運行畫面:

Copyright © Linux教程網 All Rights Reserved