User Tools

Site Tools


qt:icons

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
qt:icons [2011/05/12 09:31] – [Using Qt Resources in Qt Designer] mithatqt:icons [2011/05/13 16:50] – [Caveat] mithat
Line 1: Line 1:
 ====== Icons and Actions ====== ====== Icons and Actions ======
  
-This report discusses different ways of binding icons to actions in Qt desktop applications and provides motivation for exploring approaches to robust icon implementations. The following was written against Qt 4.7.1 and Qt Creator 2.1.0 and assumes the reader has some familiarity with Qt/C++ development.+This report discusses different ways of binding icons to actions in Qt desktop applications and provides motivation for developing approaches to robust icon implementations. The following was written against Qt 4.7.1 and Qt Creator 2.1.0 and assumes the reader has some familiarity with Qt/C++ development.
  
 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.
Line 7: Line 7:
 ===== Using Qt Resources in Qt Designer ===== ===== Using 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 additionally provides immediate visual feedback((http://wiki.forum.nokia.com/index.php/How_to_use_Resource_Editor_with_Qt_Creator)).
  
 While this approach has the above stated benefits, it's not optimal for a couple reasons. First, it lacks the ability to specify multiple icon sizes. In a typical application, icons bound to actions will need to be rendered at different sizes. For example, toolbar icons may be rendered at 24x24 pixels while menu item icons will be rendered at 16x16. As far as I can tell, Qt Designer (and possibly all of Qt) does not let you set icons for menu items and toolbar items independently. Rather, this approach relies on Qt's ability to automatically shrink (but not grow) icons to fit the desired space as needed((http://doc.qt.nokia.com/4.7/widgets-icons.html)). While this approach has the above stated benefits, it's not optimal for a couple reasons. First, it lacks the ability to specify multiple icon sizes. In a typical application, icons bound to actions will need to be rendered at different sizes. For example, toolbar icons may be rendered at 24x24 pixels while menu item icons will be rendered at 16x16. As far as I can tell, Qt Designer (and possibly all of Qt) does not let you set icons for menu items and toolbar items independently. Rather, this approach relies on Qt's ability to automatically shrink (but not grow) icons to fit the desired space as needed((http://doc.qt.nokia.com/4.7/widgets-icons.html)).
Line 96: Line 96:
 }</code> }</code>
  
-If you have more than a handful of such icons, you will probably want to write a helper function to reduce the repeated data entry.+If you have more than a handful of such icons, you will probably want to write a [[MainWindow-buildIcon|support method]] to reduce the repeated data entry.
  
 ==== Pros ==== ==== Pros ====
Line 142: Line 142:
 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.
  
-As was the case with QStyle's standardIcon() function, implementation with QIcon::fromTheme() requires a bit of hand-coding.+As was the case with QStyle'''standardIcon()'' function, implementation with ''QIcon::fromTheme()'' requires a bit of hand-coding.
  
 ==== Code implementation ==== ==== Code implementation ====
Line 199: Line 199:
  
 ==== Caveat ==== ==== Caveat ====
-QIcon's determination of the system icon theme is at the time of this writing not reliable under Linux -- at least not under Openbox; GNOME and Xfce are yet to be tested. A possible workaround is to use as yet undefined ''HeroicMeasures()''™ to suss out the actual icon theme and then set it((http://doc.qt.nokia.com/4.7/qicon.html#setThemeName, http://doc.qt.nokia.com/4.7/qicon.html#themeName)) in the constructor:+QIcon's determination of the system icon theme is at the time of this writing not reliable under Linux -- at least not under Xfce, Openbox, Fluxbox, and IceWM; GNOME seems to work as expected((http://bugreports.qt.nokia.com/browse/QTBUG-19268)). A possible workaround is to use as yet undefined ''HeroicMeasures()''™ to suss out the actual icon theme and then set it((http://doc.qt.nokia.com/4.7/qicon.html#setThemeName, http://doc.qt.nokia.com/4.7/qicon.html#themeName)) in the constructor:
 <code cpp-qt> <code cpp-qt>
 QString iconTheme = My::HeroicMeasures(); QString iconTheme = My::HeroicMeasures();
 QIcon::setThemeName(iconTheme);</code> QIcon::setThemeName(iconTheme);</code>
  
 +Until such ''HeroicMeasures()''™ are worked out or the bug is fixed, this method can't really be recommended.
 ===== Custom theme using  QIcon::fromTheme() ===== ===== Custom theme using  QIcon::fromTheme() =====
  
Line 272: Line 273:
 Note that in the set of icons I drew from, there were no 48x48 icons. To maintain best compatibility with Windows OSes, you ideally should include 48x48 icons as well((See http://msdn.microsoft.com/en-us/library/ms997636.aspx for a discussion of Windows XP icons and http://msdn.microsoft.com/en-us/library/aa511280.aspx for Windows 7.)). Note that in the set of icons I drew from, there were no 48x48 icons. To maintain best compatibility with Windows OSes, you ideally should include 48x48 icons as well((See http://msdn.microsoft.com/en-us/library/ms997636.aspx for a discussion of Windows XP icons and http://msdn.microsoft.com/en-us/library/aa511280.aspx for Windows 7.)).
  
-The pros and cons associated with this approach are essentially identical to [[#Explicitly build and bind icons at runtime]] above, but there is one important difference: you cannot combine this approach with [[#Custom theme using  QIcon::fromTheme()]] because the setting of icon themes is dynamic: the last theme name you set is the theme with which all icons will be rendered. +The pros and cons associated with this approach are essentially identical to [[#Explicitly build and bind icons at runtime]] above, but there is one important difference: you cannot combine this approach with [[#Custom theme using  QIcon::fromTheme()]] because the setting of icon themes is [[MainWindow-setActionIcon|dynamic]]: the last theme name you set is the theme with which all icons will be rendered. 
  
 ==== Code implementation ==== ==== Code implementation ====
qt/icons.txt · Last modified: 2011/05/13 17:01 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki