Chinaunix首页 | 论坛 | 博客
  • 博客访问: 421367
  • 博文数量: 32
  • 博客积分: 1843
  • 博客等级: 上尉
  • 技术积分: 634
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-25 14:30
个人简介

Nothing is impossible

文章分类

全部博文(32)

文章存档

2013年(4)

2012年(13)

2011年(6)

2008年(9)

分类: C/C++

2012-06-09 00:01:10

起因: 想將一些非圖片類型的文件,如可執行文件(exe)或批處理文件(bat)等嵌套到QT中做為資源保存,並實現可調用

疑難點:
    該問題主要疑難在於思路;網絡上有很多介紹QT資源的,多數是照著QT的文檔寫的,多為圖片類型文件如何加入資源和調用;而我要做的則不一樣(其實後來想想也是一樣的),要嵌入的非圖片類型,主要的差別在於 - 圖片類型資源可使用QT的方法直接調用,而非圖片資源則不行;
    嵌入資源是比較好解決的,但如何調用才是重點。
    我的思路是來自於國外論壇上的一個帖子,具體網址忘記了,帖子中有位大蝦提到說,其實資源在QT中也是以文件形式讀取的(QFile),但要調用的話,則應該將該資源讀出,並寫成一個臨時文件,再去調用。
    為什麼要這樣呢?其實多看看QT資源的中間文件,想想也就知道了。在QT編譯資源時,會將資源以二進制方式讀取,並整理成幾個數據結構,標識了資源的名稱、大小和內容等等。這些數據是以代碼形式鏈接到軟件裏的,雖然在讀取時可以用QFile讀取,但此時讀取的QFile仍然是代碼,而非可被調用的文件。所以就需要將QFile代碼先轉換成臨時文件,這樣才可以被調用。

類代碼:
Res2File.h

点击(此处)折叠或打开

  1. /**
  2.  *=====================================================================
  3. * Copyright (c) Power Star, Inc. All rights reserved.
  4.  *---------------------------------------------------------------------
  5.  * File Name: Res2File.h
  6.  *
  7.  * Description:
  8.  * copy the resource to temp file
  9.  *
  10.  * History:
  11.  * 2012.05.25 ryan.wang Initial version
  12.  *
  13.  *=====================================================================
  14. **/
  15. #ifndef _Res2File_h_
  16. #define _Res2File_h_
  17. #include "qtbase.h" //include many qt classes, created manually

  18. class Res2File
  19. {
  20. public:
  21.     Res2File( QString resfilename, QString subffix="" );
  22.     ~Res2File();

  23.     bool open();
  24.     void close();
  25.     void create();
  26.     void clear();
  27.     QString getfile();

  28. private:

  29.     QFile m_fileres;
  30.     QFile m_filetemp;
  31.     QFileInfo m_fileinfo;
  32.     QString m_nameres;
  33.     QString m_nametemp;
  34.     QString m_pathtemp;

  35. };

  36. #endif /* _Res2File_h_ */
Res2File.cpp

点击(此处)折叠或打开

  1. /**
  2.  *=====================================================================
  3. * Copyright (c) Power Star, Inc. All rights reserved.
  4.  *---------------------------------------------------------------------
  5.  * File Name: Res2File.cpp
  6.  *
  7.  * Description:
  8.  * copy resource to temp file
  9.  *
  10.  * History:
  11.  * 2012.05.25 ryan.wang Initial version
  12.  *
  13.  *=====================================================================
  14. **/
  15. #ifndef _Res2File_cpp_
  16. #define _Res2File_cpp_
  17. #include "Res2File.h"

  18. Res2File::Res2File( QString resfilename, QString subffix )
  19. {
  20.     //initialize
  21.     m_nameres.clear();
  22.     m_nametemp.clear();
  23.     m_pathtemp = QApplication::applicationDirPath(); //get current path of the app
  24.     m_pathtemp += QString("/");
  25.     
  26.     if( resfilename.isEmpty() ){
  27.         return;
  28.     }
  29.     m_nameres = resfilename;

  30.     if( !subffix.isEmpty() ){
  31.         m_nametemp = subffix;
  32.     }

  33.     //open();
  34. }

  35. Res2File::~Res2File()
  36. {
  37.    close();
  38. }

  39. bool Res2File::open()
  40. {
  41.     //open the resource as a QFile
  42.     m_fileres.setFileName(m_nameres);
  43.     if (!m_fileres.open (QIODevice::ReadOnly )){ //the mode should be QIODevice::ReadOnly
  44.         qDebug() << "fail to open res file";
  45.         return false;
  46.     }
  47.     else {
  48.         QFileInfo fileinfo(m_fileres); //for get the filename
  49.         qDebug() << "fileinfo name: " << fileinfo.fileName();
  50.         qDebug() << "fileinfo path: " << fileinfo.filePath();
  51.         qDebug() << "fileinfo baseName: " << fileinfo.baseName();

  52.         m_nametemp.insert(0,fileinfo.fileName()); //insert filename
  53.         m_pathtemp += m_nametemp; //add to absoulte path
  54.         qDebug() << "fileinfo full name: " << m_nametemp;
  55.         qDebug() << "fileinfo full path: " << m_pathtemp;
  56.     }

  57.     create();

  58. }

  59. void Res2File::close()

  60. {
  61.    clear();
  62. }

  63. void Res2File::create()
  64. {
  65.     //open a new file to save the data from resource
  66.     m_filetemp.setFileName(m_pathtemp);
  67.     if (!m_filetemp.open ( QIODevice::ReadWrite )){ //the mode should be include write mode
  68.         qDebug() << "fail to open temp file";
  69.         m_fileres.close();
  70.         return ;
  71.     }

  72.     m_filetemp.write(m_fileres.readAll());//拷贝

  73.     //until this time, file have been written, close files
  74.     m_filetemp.close();
  75.     m_fileres.close();
  76. }

  77. void Res2File::clear()
  78. {
  79.     if( !m_pathtemp.isEmpty() ){
  80.         m_fileinfo.setFile(m_pathtemp);
  81.         if( m_fileinfo.isFile() )
  82.             QFile::remove(m_pathtemp);//刪除文件
  83.     }
  84. }

  85. QString Res2File::getfile()
  86. {
  87.     //get file(absoulte path)
  88.     if( !m_pathtemp.isEmpty() ){
  89.         m_fileinfo.setFile(m_pathtemp);
  90.         if( m_fileinfo.isFile() )
  91.             return m_pathtemp;
  92.     }
  93.     return QString("");
  94. }

  95. #endif /* _Res2File_cpp_ */
調用範例:

点击(此处)折叠或打开

  1. Res2File *bat = new Res2File(":/white.bat"); //要用資源的別名才可以正常打開
    調用上例後,將在本地目錄下產生一個臨時文件,直到bat被刪除。

餘留問題:
    不懂為啥,在讀取資源時,非要用alias才可以正常讀取,用相對路徑就讀不出來。如果有誰知道該如何解決這問題的,請告訴我,感激不盡。

阅读(3918) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~