qt:icons
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
qt:icons [2011/05/12 09:15] – mithat | qt:icons [2011/05/13 11:19] – 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 | + | This report discusses different ways of binding icons to actions in Qt desktop applications and provides motivation for developing |
In Qt, there are (at least) five different approaches to binding icons((http:// | In Qt, there are (at least) five different approaches to binding icons((http:// | ||
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, | + | The path-of-least-resistance for binding icons to actions when developing Qt applications with Qt Designer((Throughout, |
- | 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, | + | 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, |
- | Developers using this approach typically provide an icon in a resource that is as large as the application is likely to require, and then let Qt resize it as needed. Thus, for example, you might bind a 48x48 icon to an action and let Qt shrink it to 24x24 for the toolbar and 16x16 for the menu item. While this works, the shrunk icons often appear fuzzy or otherwise suboptimal((As near as I can tell, it's not possible to specify multi-sized icons in Qt Designer. You can supply Windows-style multi-size '' | + | Developers using this approach typically provide an icon in a resource that is as large as the application is likely to require, and then let Qt resize it as needed. Thus, for example, you might bind a 48x48 icon to an action and let Qt shrink it to 24x24 for the toolbar and 16x16 for the menu item. While this works, the shrunk icons often appear fuzzy and/or otherwise suboptimal((As near as I can tell, it's not possible to specify multi-sized icons in Qt Designer. You can supply Windows-style multi-size '' |
FIXME (an image here would help a lot) | FIXME (an image here would help a lot) | ||
Line 17: | Line 17: | ||
The second issue with the way Qt Designer deals with binding icons to actions is that it doesn' | The second issue with the way Qt Designer deals with binding icons to actions is that it doesn' | ||
- | === Code implementation === | + | ==== Code implementation |
There is no specific code implementation required to use the above icon bindings apart from what Qt Designer and '' | There is no specific code implementation required to use the above icon bindings apart from what Qt Designer and '' | ||
<code cpp-qt> | <code cpp-qt> | ||
Line 28: | Line 28: | ||
}</ | }</ | ||
- | === Pros === | + | ==== Pros ==== |
* Available on all platforms | * Available on all platforms | ||
* Incredibly easy; no coding required | * Incredibly easy; no coding required | ||
- | === Cons === | + | ==== Cons ==== |
* Poor resizing | * Poor resizing | ||
* Not consistent with system icon theme | * Not consistent with system icon theme | ||
Line 40: | Line 40: | ||
It's possible to build '' | It's possible to build '' | ||
- | === Code implementation === | + | ==== Code implementation |
In the code below, we are assuming we have a resource in our project root called '' | In the code below, we are assuming we have a resource in our project root called '' | ||
Line 72: | Line 72: | ||
": | ": | ||
documentOpenIcon.addFile( | documentOpenIcon.addFile( | ||
- | ": | + | ": |
ui-> | ui-> | ||
Line 81: | Line 81: | ||
": | ": | ||
saveAsIcon.addFile( | saveAsIcon.addFile( | ||
- | ": | + | ": |
ui-> | ui-> | ||
Line 90: | Line 90: | ||
": | ": | ||
exitIcon.addFile( | exitIcon.addFile( | ||
- | ": | + | ": |
ui-> | ui-> | ||
Line 96: | Line 96: | ||
}</ | }</ | ||
- | If you have more than a handful of such icons, you will probably want to write a helper function | + | If you have more than a handful of such icons, you will probably want to write a [[MainWindow-buildIcon|support method]] |
- | === Pros === | + | ==== Pros ==== |
* Available to all platforms | * Available to all platforms | ||
* Excellent resizing (if you provide all required sizes) | * Excellent resizing (if you provide all required sizes) | ||
* Covers all needed icons (if you provide all required icons) | * Covers all needed icons (if you provide all required icons) | ||
- | === Cons === | + | ==== Cons ==== |
* Not consistent with system icon theme | * Not consistent with system icon theme | ||
* Requires lots of coding | * Requires lots of coding | ||
Line 115: | Line 115: | ||
Implementation requires only a bit of straight-forward hand-coding. | Implementation requires only a bit of straight-forward hand-coding. | ||
- | === Code implementation === | + | ==== Code implementation |
In our example, acceptable '' | In our example, acceptable '' | ||
Line 130: | Line 130: | ||
}</ | }</ | ||
- | === Pros === | + | ==== Pros ==== |
* Consistent with system icon theme | * Consistent with system icon theme | ||
* Available on all platforms | * Available on all platforms | ||
* Fairly easy to implement | * Fairly easy to implement | ||
- | === Cons === | + | ==== Cons ==== |
* Poor resizing | * Poor resizing | ||
* Limited range of icons | * Limited range of icons | ||
===== System theme using QIcon:: | ===== System theme using QIcon:: | ||
- | On X11 systems, Qt lets you access all the icons that are part of the system icon theme using the static function '' | + | On X11 systems, Qt lets you access all the icons that are part of the system icon theme using the static function '' |
As was the case with QStyle' | As was the case with QStyle' | ||
- | === Code implementation === | + | ==== Code implementation |
In our example, using '' | In our example, using '' | ||
Line 189: | Line 189: | ||
- | === Pros === | + | ==== Pros ==== |
* Consistent with system icon theme | * Consistent with system icon theme | ||
* Excellent resizing (depending on icon theme) | * Excellent resizing (depending on icon theme) | ||
* Fairly easy to implement | * Fairly easy to implement | ||
- | === Cons === | + | ==== Cons ==== |
* Available only on X11 systems | * Available only on X11 systems | ||
* Wide range of icons, but won't cover all requirements | * Wide range of icons, but won't cover all requirements | ||
- | === Caveat === | + | ==== Caveat |
- | QIcon' | + | QIcon' |
<code cpp-qt> | <code cpp-qt> | ||
QString iconTheme = My:: | QString iconTheme = My:: | ||
Line 274: | Line 274: | ||
The pros and cons associated with this approach are essentially identical to [[# | The pros and cons associated with this approach are essentially identical to [[# | ||
- | === Code implementation === | + | ==== Code implementation |
Once you have created your custom icon theme and added it to your project as a resource, using it is essentially identical to using the system icon theme except that you must now specify the theme name((http:// | Once you have created your custom icon theme and added it to your project as a resource, using it is essentially identical to using the system icon theme except that you must now specify the theme name((http:// | ||
<code cpp-qt> | <code cpp-qt> | ||
Line 292: | Line 292: | ||
You can (and typically should) use the forms shown above for testing icon existence and/or setting fallback icons. | You can (and typically should) use the forms shown above for testing icon existence and/or setting fallback icons. | ||
- | === Pros === | + | ==== Pros ==== |
* Available to all platforms | * Available to all platforms | ||
* Excellent resizing (if you provide all required sizes) | * Excellent resizing (if you provide all required sizes) | ||
* Covers all needed icons (if you provide all required icons) | * Covers all needed icons (if you provide all required icons) | ||
- | === Cons === | + | ==== Cons ==== |
* Not consistent with system icon theme | * Not consistent with system icon theme | ||
* PITA to implement | * PITA to implement |
qt/icons.txt · Last modified: 2011/05/13 17:01 by mithat