【轉】設置Qt應用程序圖標及應用程序名


一直以來很糾結給qt應用程序添加圖標問題,在網上收過一次,但是感覺不夠完整,現將自己的實現過程記錄下,以便以后查看:

通過網上的例子知道qt助手中有相關說明:

Setting the Application Icon

The application icon, typically displayed in the top-left corner of an application's top-level windows, is set by calling theQWidget::setWindowIcon() method on top-level widgets.

In order to change the icon of the executable application file itself, as it is presented on the desktop (i.e., prior to application execution), it is necessary to employ another, platform-dependent technique.

Setting the Application Icon on Windows

First, create an ICO format bitmap file that contains the icon image. This can be done with e.g. Microsoft Visual C++: SelectFile|New, then select the File tab in the dialog that appears, and choose Icon. (Note that you do not need to load your application into Visual C++; here we are only using the icon editor.)

Store the ICO file in your application's source code directory, for example, with the name myappico.ico. Then, create a text file called, say, myapp.rc in which you put a single line of text:

 IDI_ICON1               ICON    DISCARDABLE     "myappico.ico"

Finally, assuming you are using qmake to generate your makefiles, add this line to your myapp.pro file:

 RC_FILE = myapp.rc

Regenerate your makefile and your application. The .exe file will now be represented with your icon in Explorer.

If you do not use qmake, the necessary steps are: first, run the rc program on the .rc file, then link your application with the resulting .res file.

 

從上面可將方法分為兩種:

1.使用軟件的方法可設置程序窗口的默認圖標,但是它無法改變應用程序文件.exe的圖標。

2.使用qmake生成makefile的,如qt+eclipse,qt creator通過”If you do not use qmake"之前的方法就可以解決

3.使用qt+vs2010不是用qmake的情況,需要執行"If you do not use qmake..."方法,先將.rc文件添加到工程中,再編譯.rc文件,最后重新連接下即可改變圖標。

實現過程:

1.設置應用程序運行時所有窗口默認圖標,

 

[cpp]  view plain copy
 
  1. QApplication a(argc, argv);  
  2. //獲得可執行程序路徑  
[cpp]  view plain copy
 
  1. QString dir = QApplication::applicationDirPath();  
  2. //設置可執行程序路徑為當前工作路徑  
  3. QDir::setCurrent(dir);  
  4. QApplication::addLibraryPath("./plugins");  
[cpp]  view plain copy
 
  1. QApplication::addLibraryPath("./images");  
  2. a.setWindowIcon(QIcon("./images/myappico.ico"));  
2.通過qmake生成makefile實現過程:

 

a.找到一張圖片.ico,名字改為myappico.ico;

b.創建一個新的文本文檔,內部添加  IDI_ICON1           ICON   DISCARDABLE   "myappico.ico",並將文件重命名為myapp.rc;

c.在myapp.pro文件最后加上RC_FILE = myapp.rc,重新生成之后,就修改成功了
3.不用qmake生成makefile實現過程:

前面兩步驟一樣,最后一步改為,將.rc文件加載至工程中,通過右鍵工程——添加——已存在文件,添加后右鍵.rc文件編譯,重新生成可執行文件后就修改成功了


免責聲明!

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



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