您的位置:首页 > 移动开发

Qt Application Icon Setting(Qt在不同平台下的图标设置)

2016-07-29 17:05 597 查看
英文原文:
Setting the Application Icon

The application icon, typically displayed in the top-leftcorner of an application's top-level windows, is set by calling theQWindow::setWindowIcon() method.
In order to changethe icon of the executable application file itself, as it is presented on thedesktop (i.e., prior to application execution), it is necessary to employanother, platform-dependent technique.

Setting the Application Icon on Windows

First, create anICO format bitmap file that contains the icon image. This can be done with e.g.Microsoft Visual C++: SelectFile|New, then select the File tabin the dialog that appears, and choose Icon.
(Note that you do notneed to load your application into Visual C++; here we are only using the iconeditor.)
Store the ICO filein your application's source code directory, for example, with the name myappico.ico.
Then, assuming youare using qmake to generate your makefiles, you only need to add a single lineto your .pro projectfile:

RC_ICONS = myappico.ico

Finally, regenerateyour makefile and your application. The .exe file will now be represented by your icon inExplorer.
However, if youalready have an .rc file, forexample, with the name myapp.rc, which you wantto reuse, the following two steps will be required. First, put a single line oftext to the myapp.rc file:

IDI_ICON1              ICON    DISCARDABLE     "myappico.ico"

Then, add thisline to your myapp.pro file:

RC_FILE = myapp.rc

If you do notuse qmake, the necessarysteps are: first, create an .rc file and runthe rc or windres program on the .rc file, then link your application with theresulting .res file.

Setting the Application Icon on OS X

The applicationicon, typically displayed in the application dock area, is set by callingQWindow::setWindowIcon() on a window. It is possible that the program couldappear in the application dock area before the function call, in which case adefault
icon will appear during the bouncing animation.
To ensure that thecorrect icon appears, both when the application is being launched, and in theFinder, it is necessary to employ a platform-dependent technique.
Although manyprograms can create icon files (.icns), the recommendedapproach is to use the Icon Composer program supplied by Apple(in theDeveloper/Application folder). IconComposer allows you to import several different sized
icons (for usein different contexts) as well as the masks that go with them. Save the set oficons to a file in your project directory.
If you are usingqmake to generate your makefiles, you only need to add a single line toyour .pro projectfile. For example, if the name of your icon file is myapp.icns, and your project file is myapp.pro, add this line to myapp.pro:

ICON = myapp.icns

This will ensurethat qmake puts youricons in the proper place and creates an Info.plist entry for the icon.
If you do notuse qmake, you must do thefollowing manually:
1.   Create an Info.plist file for your application (using the PropertyListEditor, found in Developer/Applications).
2.   Associate your .icns record with the CFBundleIconFile record in the Info.plist file (again, using the PropertyListEditor).
3.   Copy the Info.plist file into your application bundle's Contents directory.
4.   Copy the .icns file into your application bundle's Contents/Resources directory.

Setting the Application Icon on CommonLinux Desktops

In this section webriefly describe the issues involved in providing icons for applications fortwo common Linux desktop environments: KDE and GNOME.
The coretechnology used to describe application icons is the same for both desktops,and may also apply to others, but there are details which are specific to each.The main source of information on the standards used by these Linux desktopsis freedesktop.org.
For informationon other Linux desktops please refer to the documentation for the desktops youare interested in.
Often, users donot use executable files directly, but instead launch applications by clickingicons on the desktop. These icons are representations of "desktop entryfiles" that contain a description of the application that includesinformation
about its icon. Both desktop environments are able to retrieve theinformation in these files, and they use it to generate shortcuts toapplications on the desktop, in the start menu, and on the panel.
More informationabout desktop entry files can be found in the DesktopEntry Specification.
Although desktopentry files can usefully encapsulate the application's details, we still needto store the icons in the conventional location for each desktop environment. Anumber of locations for icons are given in the IconTheme
Specification.
Although the pathused to locate icons depends on the desktop in use, and on its configuration,the directory structure beneath each of these should follow the same pattern:subdirectories are arranged by theme, icon size, and application type.Generally,
application icons are added to the hicolor theme, so a squareapplication icon 32 pixels in size would be stored in the hicolor/32x32/apps directory beneath the icon path.
K DesktopEnvironment (KDE)
Application iconscan be installed for use by all users, or on a per-user basis. A user currentlylogged into their KDE 4 desktop can discover these locations by usingkde4-config, for example, by typing the following in a terminal window:

kde4-config --path icon

Applications usingQt 5 and KDE Frameworks 5 will find their icons in the list returned by thiscommand:

qtpaths --locate-dirs GenericDataLocation icons

Typically, thelist of colon-separated paths printed to stdout includes the user-specific iconpath and the system-wide path. Beneath these directories, it should be possibleto locate and install icons according to the conventions described in
the IconTheme Specification.
If you aredeveloping exclusively for KDE, you may wish to take advantage of the KDEbuild system to configure
your application. This ensures thatyour icons are installed in the appropriate locations for KDE.
The KDE developerwebsite is at http://techbase.kde.org/.
GNOME
Application iconsare stored within a standard system-wide directory containingarchitecture-independent files. This location can be determined by using gnome-config, for example by typing the following in aterminal window:

gnome-config --datadir

The path printedon stdout refers to a location that should contain a directory called pixmaps; the directory structure within the pixmaps directory is described in theIconTheme
Specification.
If you aredeveloping exclusively for GNOME, you may wish to use the standard set of GNUBuild Tools, also described in
the relevant section of the GTK+/GnomeApplication Development book. This ensures that your icons areinstalled in the appropriate
locations for GNOME.
The GNOMEdeveloper website is at http://developer.gnome.org/.

中文翻译,翻译的不准确,请大家多家指教,我会更加努力。

设置应用程序的图标
         Qt应用程序的图标,典型的情况下是在窗口左边的顶部,我们可以通过调用Qwindow::setwindowIcon方法设置。为了达到为执行程序制作桌面图标的目的(在程序执行之前),那是有必要采用另一类依靠平台的应用技术。

Windows平台设置应用程序图标

         1.首先创建位图格式的位图文件,其中包含图标图片,它可以通过微软的VC++制作:选择File|New,然后在出现的对话框中选择File 选项卡(译者:VC++已经不用,没试过,记忆中好像是可以得),并且选择图标(记住你不需要加载的应用程序到VC++中,这里我们仅仅是使用图标编辑器)。

         2.图标文件存储在你应用程序代码文件夹中。比如:名字叫做 :myappico.ico,然后,假如你是用qmake生成你的makefile文件的,你仅仅需要添加单独一行到你的.pro文件中,

RC_ICONS= myappico.ico,最后从新编译makefile文件和应用程序,从资源管理中查看,

应用程序图标已经被替换了,但是如果你已经有了.rc文件,例如:名字是myapp.rc,如果你想使用它,需要以下两个步骤:

首先:你要放单独一行文本到myapp.rc文件中,

IDI_ICON1        ICON         DISCARDABLE                   “myapp.rc”,

然后增加一行到你的.pro文件中:RC_FILE:myapp.rc

 

注:如果你不想使用qmake的话,那么以下步骤是必须的:首先,创建一个.rc文件,并运行.rc文件,或者windows程序运行.rc文件,然后链接你的应用程序到 .res文件。

 

OS X 平台设置应用程序图标

         应用程序图标,典型的情况下是出现在应用的dock区域,可以通过Qwindow::setwindowIcon设置,也可能在应用程序启动前被设置在dock区域,在这种情况下可能以动画的形式出现。为了保证图标正常的出现,程序在开始启动中,必须依靠平台技术。

         虽然很多程序可以创建图标文件(.icns),但是我们推荐使用由苹果提供的图标设计程序(在
Developer/Application
 文件夹中),Iconcomposer允许你导入不同一系列不同大小的图标(运用在不同的环境中),并且可以编辑他们,保存设置的图标到工程目录中。

        如果你使用qmake生成你的makefile文件,你仅仅需要添加单独一行到你的.pro文件中,比如的图标名字叫做myapp.icns,并且你的工程名字叫做myapp.pro,增加一下一行到你的myapp.pro中:

ICON= myapp.icns         

确保qmake中你的图标放在合适的位置,并且创建一个Info.plist文件作为图标的入口。

         如果你不使用qmake的方法,你必须手动使用以下几种方式:

1.      为你的运用创建一个Info.plist文件(使用PropertyListEditor,可以在Developer/Applications中找到)。

2.      和CFBundleIconFile文件一起记录你的图标在Info.plist文件中(再次使用PropertyListEditor)。

3.      拷贝Info.plist文件到你的应用的捆绑目录中。

4.      拷贝的.icns文件到你应用的捆绑目录的资源目录下。

 

为一般的Linux程序设置桌面图标

         本节内容我们将要简介的描述相关两种为Linux桌面环境下制作图标的方法:KDE和GNMOE,他们的核心技术应用在桌面程序上是一样的,在具体细节上也可能使用到其他技术,信息的主要来源是linux的使用标准,freedesktop.org,如果有兴趣的话可以参考其他关于桌面的信息的文章。

         通常情况下用户是不适用可执行文件的,但是代替它的一般是在桌面点击图标,这些图标是表示桌面入口文件用来描述一个运用,包含图标信息,桌面环境是用来启动这些文件信息的,并且他们是桌面上一个执行程序的快捷方式,在开始菜单或者面板中。

更多的想了解桌面程序入口文件请登陆:Desktop Entry Specification.

         虽然桌面入口文件通常是用来封装运用的一些细节,我们任然需要存储图标在常规的桌面环境中。

         一些图标主题可以在IconTheme Specification.中寻找。

       虽然运用程序路径取决于什么样的桌面使用,它的配置,下面的目录结构每一个应该遵循相同的模式:子目录是按照,主题、图标大小、和应用的类型排序,一般情况下,应用程序的图标被增加hicolor的主题,因此一个32像素的正方形的图标是被存在hicollor/32x32/apps目录下的图标路径。

kDE桌面环境

         应用程序图标可以被给所有的安装用户使用,一个用户可以正确的登陆到他的KDE环境下,但是应该配置使用kde4-config配置,比如,可以使用以下主题窗口:

Kde4–config –path icon

         用户使用qt5和KDE 5框架通过以下的命令可以在返回的列表中找到图标:

qtpaths--locate-dirs GenericDataLocation icons

         典型情况下,可以使用执行搜索路径的列表的形式将用户的图标和系统的图标输出打印出来,在他们的目录之下,根据IconTheme Specification.的描述,将要能够定位和安装图标的。

       如果你的开发仅仅是为了KDE,你或许希望采用优秀的KDE构建系统的方法构建的你的应用,这确保你的图标是正确的安装在KDE下。

       KDE开发网站:http://techbase.kde.org/.

GNOME

         应用程序的图标被存储在标准的系统文件目录接口下并且包含与体系结构无关的文件,这些位置取决于用户用户gnome 配置,例如,可以使用以下窗口主题:

gnome-config –datadir

通过调用pixmaps路径的相关目录结构是可以被打印出来的,目录结构在pixmaps详细描述可以参考IconTheme Specification.

       如果你开发是为了GNOME,或者希望使用GNUBuild Tool标准设置,有关章节的描述可以在GTK+/Gnome ApplicationDevelopment book中找到,这样可以保证你的应用是安装正确的在GNOME中。

 GNOME开发网站是:http://developer.gnome.org/.

                                                                                             联系QQ:2120263292  欢迎开发软件、 交流     
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: