User Tools

Site Tools


qt:toward_robust_icon_support

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
qt:toward_robust_icon_support [2018/08/03 19:40] – [Icons and Actions] mithatqt:toward_robust_icon_support [2018/08/03 19:46] (current) mithat
Line 11: Line 11:
 In Qt, there are (at least) five different approaches to binding icons((http://doc.trolltech.com/4.7/qicon.html)) to actions((http://doc.trolltech.com/4.7/qaction.html)), each offering different pros and cons with respect to development time, system integration, and icon rendering quality. The different approaches are described below. For purposes of demonstration in the following discussions, I will assume we have a main window with three actions: ''action_Open'' that opens a file, ''action_Convert'' that converts the open file and saves it, and ''action_Quit'' that exits the application. In Qt, there are (at least) five different approaches to binding icons((http://doc.trolltech.com/4.7/qicon.html)) to actions((http://doc.trolltech.com/4.7/qaction.html)), each offering different pros and cons with respect to development time, system integration, and icon rendering quality. The different approaches are described below. For purposes of demonstration in the following discussions, I will assume we have a main window with three actions: ''action_Open'' that opens a file, ''action_Convert'' that converts the open file and saves it, and ''action_Quit'' that exits the application.
  
-==== Using Qt Resources in Qt Designer ====+==== Option 1: Use Qt Resources in Qt Designer ====
  
 The path-of-least-resistance for binding icons to actions when developing Qt applications with Qt Designer((Throughout, I use Qt Designer to refer both to the standalone application as well as its built-in support for the the version embedded into Qt Creator)) is to use Qt Resources((http://doc.qt.nokia.com/4.7/resources.html)). The support for ''*.qrc'' resource files built into Qt Designer makes adding icon resources and binding them to actions very easy and provides immediate visual feedback((http://wiki.forum.nokia.com/index.php/How_to_use_Resource_Editor_with_Qt_Creator)). The path-of-least-resistance for binding icons to actions when developing Qt applications with Qt Designer((Throughout, I use Qt Designer to refer both to the standalone application as well as its built-in support for the the version embedded into Qt Creator)) is to use Qt Resources((http://doc.qt.nokia.com/4.7/resources.html)). The support for ''*.qrc'' resource files built into Qt Designer makes adding icon resources and binding them to actions very easy and provides immediate visual feedback((http://wiki.forum.nokia.com/index.php/How_to_use_Resource_Editor_with_Qt_Creator)).
Line 42: Line 42:
   * Not consistent with system icon theme   * Not consistent with system icon theme
  
-==== Explicitly build and bind icons at runtime ====+==== Option 2: Explicitly build and bind icons at runtime ====
  
 It's possible to build ''QIcon'' instances in code that encapsulate multiple icon sizes. Once this is done, the ''QIcon'' instance can be bound to an action -- and Qt will automatically select the best size for a given rendering. This requires that the developer build a resource tree with images for all the required sizes and then do a fair amount of hand-coding for each icon. It's possible to build ''QIcon'' instances in code that encapsulate multiple icon sizes. Once this is done, the ''QIcon'' instance can be bound to an action -- and Qt will automatically select the best size for a given rendering. This requires that the developer build a resource tree with images for all the required sizes and then do a fair amount of hand-coding for each icon.
Line 113: Line 113:
   * Requires lots of coding   * Requires lots of coding
  
-==== QStyle's standardIcon() function ====+==== Option 3: QStyle's standardIcon() function ====
  
 An alternative to using icon resources is provided by the ''standardIcon()'' function, which is an instance member of the ''QStyle'' abstract base class((http://doc.qt.nokia.com/4.7/qstyle.html#standardIcon)). This function provides access to the subset of standard system icons enumerated in ''StandardPixmap-enum''((http://doc.qt.nokia.com/4.7/qstyle.html#StandardPixmap-enum)). An alternative to using icon resources is provided by the ''standardIcon()'' function, which is an instance member of the ''QStyle'' abstract base class((http://doc.qt.nokia.com/4.7/qstyle.html#standardIcon)). This function provides access to the subset of standard system icons enumerated in ''StandardPixmap-enum''((http://doc.qt.nokia.com/4.7/qstyle.html#StandardPixmap-enum)).
Line 145: Line 145:
   * Limited range of icons   * Limited range of icons
  
-==== System theme using  QIcon::fromTheme() ====+==== Option 4: Use the system theme with  QIcon::fromTheme() ====
 On X11 systems, Qt lets you access all the icons that are part of the system icon theme using the static function ''QIcon::fromTheme()''.((http://doc.qt.nokia.com/4.7/qicon.html#fromTheme)) Icons so accessed will be selected as needed based on the required rendering size. On X11 systems, Qt lets you access all the icons that are part of the system icon theme using the static function ''QIcon::fromTheme()''.((http://doc.qt.nokia.com/4.7/qicon.html#fromTheme)) Icons so accessed will be selected as needed based on the required rendering size.
  
Line 210: Line 210:
 QIcon::setThemeName(iconTheme);</code> QIcon::setThemeName(iconTheme);</code>
  
-==== Custom theme using  QIcon::fromTheme() ====+==== Option 5: Use a custom theme with  QIcon::fromTheme() ====
  
 The static function ''QIcon::fromTheme()'' used above can also be used with custom, locally defined icon themes that are placed into Qt Resource files((This idea comes from http://tkrotoff.blogspot.com/2010/02/qiconfromtheme-under-windows.html)). To accomplish this we need to do three things: Create a custom icon theme, create a Qt Resource with the theme, and implement the code needed to use the custom theme. The static function ''QIcon::fromTheme()'' used above can also be used with custom, locally defined icon themes that are placed into Qt Resource files((This idea comes from http://tkrotoff.blogspot.com/2010/02/qiconfromtheme-under-windows.html)). To accomplish this we need to do three things: Create a custom icon theme, create a Qt Resource with the theme, and implement the code needed to use the custom theme.
qt/toward_robust_icon_support.txt · Last modified: 2018/08/03 19:46 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki