How to embed an icon in a Qt application

本文将演示如何在Qt应用中嵌入外部文件,如图标,通过创建Qt资源集合文件并引用实现资源的高效管理和应用。

This approach holds for any type of an external file that needs to be embedded in an executable, such as an image (icon), a text file containing help etc. The embedding is accomplished by creating a Qt resource collection file *.qrc that lists the files that will become the part of the application source tree.

I am going to demonstrate the problem on an icon alignLeft.png that needs to be embedded in an application. To achieve the embedding we need to perform the following steps in Qt Creator:

  1. In the project folder create a folder 'icons'. Place 'alignLeft.png' into the folder 'icons'.
  2. To create a resource file: Right-click on the highlighted project -> Add New -> Qt -> Qt Resource file. Name the resource file 'qtresourceexample.qrc'.
  3. To edit the resource file: Right-click on qtresourceexample.qrc -> Open With -> Plain Text Editor. Enter the following lines into the resource file:
    <!DOCTYPE RCC><RCC version="1.0">
     <qresource>
         <file>icons/alignLeft.png</file>
     </qresource>
     </RCC>
  4. Finally, check whether the resource file appeared in the *.pro file.
    SOURCES += \
        main.cpp \
        mainwindow.cpp
    
    HEADERS += \
        mainwindow.h
    
    RESOURCES += \
        qtresourceexample.qrc
    
  5. In the code we refer to the icon in the following way:
    alignLeftAction->setIcon(QIcon(":/icons/alignLeft.png"));
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值