Qt4--创建自定义图标

1、通过Qt Assistant 查看 Qt resource system

原文:The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable. This is useful if your application always needs a certain set of files (icons, translation files, etc.) and you don't want to run the risk of losing the files.


意思是说,Qt resource system是和平台无关,他用来为应用程序存储二进制文件。如果在运行过程中不想要冒文件丢失的险,这样的文件集就是一个很好的选择。


原文:The resources associated with an application are specified in a .qrc file, an XML-based file format that lists files on the disk and optionally assigns them a resource name that the application must use to access the resource.

Here's an example .qrc file:

<!DOCTYPE RCC><RCC version="1.0">
 <qresource>
     <file>images/copy.png</file>
     <file>images/cut.png</file>
     <file>images/new.png</file>
     <file>images/open.png</file>
     <file>images/paste.png</file>
     <file>images/save.png</file>
 </qresource>
 </RCC>
这些资源被存放在.qrc文件下,这是一个基于XML格式的文件。

原文:For a resource to be compiled into the binary the .qrc file must be mentioned in the application's .pro file so that qmake knows about it. For example:

RESOURCES     = application.qrc
需要在.pro文件内加入.qrc文件

如下图是qmake以后生产的文件。也就是程序中可以用如“:/images/copy.png”



原文:

Using Resources in the Application

In the application, resource paths can be used in most places instead of ordinary file system paths. In particular, you can pass a resource path instead of a file name to the QIconQImage, or QPixmap constructor:

cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);


2、示例
1、在目录下建立images文件夹,在文件夹内放入图片:buttonico.png



2、建立一个Qt resource 文件,取名为images.qrc



3、修改images.qrc文件内容


修改好以后Qt Creator内images.qrc发生变化



4、创建main.cpp

#include<QApplication>
#include<QPushButton>

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    QPushButton* button = new QPushButton();
    button->setIcon(QIcon(":/images/buttonico.png"));
    QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()));
    button->show();
    return app.exec();
}

5、生成结果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值