How to embed an icon in a Qt application
2015-09-21 15:50
417 查看
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.
Qt4 or Qt5 compatible
qtresourceexample/mainwindow.h
qtresourceexample/mainwindow.cpp
qtresourceexample/main.cpp
qtresourceexample/qtresourceexample.qrc
qtresourceexample/icons/alignLeft.png
qtresourceexample/qtresourceexample.pro (Qt 4 compatible)
qtresourceexample/qtresourceexample.pro (Qt 5 compatible)
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:
In the project folder create a folder 'icons'. Place 'alignLeft.png' into the folder 'icons'.
To create a resource file: Right-click on the highlighted project -> Add New -> Qt -> Qt Resource file. Name the resource file 'qtresourceexample.qrc'.
To edit the resource file: Right-click on qtresourceexample.qrc -> Open With -> Plain Text Editor. Enter the following lines into the resource file:
Finally, check whether the resource file appeared in the *.pro file.
In the code we refer to the icon in the following way:
that will become the part of the application source tree.
Qt4 or Qt5 compatible
qtresourceexample/mainwindow.h
qtresourceexample/mainwindow.cpp
qtresourceexample/main.cpp
qtresourceexample/qtresourceexample.qrc
qtresourceexample/icons/alignLeft.png
qtresourceexample/qtresourceexample.pro (Qt 4 compatible)
qtresourceexample/qtresourceexample.pro (Qt 5 compatible)
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:
In the project folder create a folder 'icons'. Place 'alignLeft.png' into the folder 'icons'.
To create a resource file: Right-click on the highlighted project -> Add New -> Qt -> Qt Resource file. Name the resource file 'qtresourceexample.qrc'.
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>
Finally, check whether the resource file appeared in the *.pro file.
SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h RESOURCES += \ qtresourceexample.qrc
In the code we refer to the icon in the following way:
alignLeftAction->setIcon(QIcon(":/icons/alignLeft.png"));
相关文章推荐
- Android快速开发工具GsonFormat使用教程
- android弹出下拉选择菜单,单选,多选【运行截图——图】
- android Bitmap getByteCount和getRowBytes
- android studio集成svn
- ios数据存储之归档
- iOS 之旅--视频收获笔记(持续更新)
- iOS#import与@class的区别
- 解决android代码中重复写findViewByid()
- 微信支付之认识微信支付流程
- 微信支付之认识微信支付开发
- 【Unity3D】利用Shader以及更改Mesh实现2D游戏的动态阴影效果
- 生成小学算术题——小程序
- android客户端性能测试 (一)
- Android 性能测试
- IOS学习笔记——Obj-c中实例方法与类方法的区别
- iOS—NSLog各种打印格式
- Android 官方推荐 : DialogFragment 创建对话框
- android布局相关
- iOS--进阶知识--Runtime
- android studio ndk