当前位置:编程学习 > html/css >>

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> 

<!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 

 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 QIcon, QImage, or QPixmap constructor:

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

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(); 
} 

 

补充:web前端 , HTML/CSS  ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,