Qt uses native style APIs on each supported platform, however there are some additional tricks you can use to make sure your Qt-based application looks, feels and behaves better. This presentation will run through examples, tips and ticks to help you make your applications look great on all platforms.
Presentation by Jens Bache-Wiig held during Qt Developer Days 2009.
http://qt.nokia.com/developer/learning/elearning
5. Introduction
• Platform guidelines
– Windows User Experience Interaction Guidelines
– Apple Human Interface Guidelines
– KDE User Interface Guidelines
– GNOME HIG
5
8. QStyle
• Do not force a specific style
• Ship all available styles
– Make sure to compile with GTK+ headers on X11 and
the Windows Vista SDK on windows
• Always use the style when implementing custom
widgets
8
27. Dialogs
QDialogButtonBox
- Manages order, layout, icon and text
QDialogButtonBox box(QDialogButtonBox::Save |
QDalogButtonBox::Discard |
QDialogButtonBox::Cancel);
27
28. Dialogs
• Custom buttons are marked with a role
– Determines positioning
– Reject, accept, help …
QDialogButtonBox box;
box.addButton(myButton, QDialogButtonBox::AcceptRole);
28
29. Dialogs
Traditional modal dialog
MyQDialogSubclass dialog;
// Various bits of initialization
if (dialog.exec() == QDialog::Accept) {
// Set new values or do extra work
// based on results.
}
29
30. Dialogs
• Windows
– Use only for critical or infrequent one-off tasks that
require completion before continuing
• KDE
– Use only if allowing interaction would cause data loss
or some other serious problem
30
45. Cross platform tips
• QDesktopServices::storageLocation()
– gives you default system directories such as
Desktop, Music, Pictures, Applications, Temp and
Cache
45
46. Cross platform tips
How can you open an e-mail using your standard e-mail
application?
46
47. Cross platform tips
• QDesktopServices::openUrl(const QUrl &url)
– Launches the provided URL using the default system
application
openUrl(“mailto:myself@gmail.com”);
openUrl(“http://qt.nokia.com”);
openUrl(QUrl::fromLocalFile(…));
47
49. Cross platform tips
• Depends on your platform!
• Use standard shortcuts whenever possible
• Test for conflicts on all platforms
QAction action;
action.setShortcuts(QKeySequence::Find);
49
50. Cross platform tips
// Get a list of all keys for a StandardKey.
QList<QKeySequence> keys =
QKeySequence::keyBindings(QKeySequence::NextChild);
foreach (QKeySequence key, keys) {
printOut(key.toString(QKeySequence::PortableText));
} …
50
51. Cross platform tips
• Use a consistent icon theme
• Lots of free themes available online
– Oxygen, Tango to mention a few
http://www.oxygen-icons.org http://tango.freedesktop.org
51
52. Cross platform tips
• Icon theme support in 4.6
QIcon::fromTheme(“document-edit”);
QIcon::fromTheme(“document-edit”,
QIcon(“document.png”));
For previous versions of Qt: http://code.google.com/p/qticonloader/
52
58. Cross platform tips
• Icons in menus
– Not visible on Mac
– Visible on Windows and KDE
– Depends on the system setting in GNOME
• Override with
– QAction::setIconVisibleInMenu(bool)
– QApplication::setAttribute(Qt::AA_DontShowIconsInMenus)
58
59. Cross platform tips - Dialogs
• Preferences on GNOME/Mac
– Applies immediately
• Preferences on Windows/KDE
– Apply/Cancel
59
60. Cross platform tips
• MDI interfaces
– Mac does not support it
– GTK+ does not support it
– Microsoft:
• SDI is appropriate for most productivity applications. MDI is
still in use, but does not fit as well with today's users and
operating systems
60
61. Go native!
• Ifdef is evil but sometimes useful…
– Q_WS_WIN
– Q_WS_MAC
– Q_WS_X11
61
62. The window id
– QWidget::winId()
– Returns a native window handle
• HWND on Windows
• NSView* on Cocoa
• X11 handle
– Allows using native API
• Windows Vista or Windows 7 specific features
62
71. Mac – Doc menu
QMenu*menu = new QMenu;
// Add actions to the menu
// Connect them to slots
...
extern void qt_mac_set_dock_menu(QMenu *);
qt_mac_set_dock_menu(menu);
71
72. Mac
• Qt automatically rearranges menu entries
– Based on name: about, settings, preferences, quit, exit
– Override with QAction::menuRole
• AboutRole, PreferencesRole, QuitRole, NoRole (do not move)
• Example
• A QMenu entry called “settings” goes to
Application::preferences on mac
72
74. X11
• Follow freedesktop.org standards if possible
– Menu specs
– Icon themes
– Autostart
– Bookmarks
– .desktop file
74
75. X11
• Make a desktop file
– standards.freedesktop.org/desktop-entry-spec/
– Simple configuration file containing
• Application Icon
• Menu Entry
• Registered mime types
• …
75
76. X11
• How do you know if you are running KDE or
GNOME?
– No 100% reliable way of doing it
– You can try the “DESKTOP_SESSION” env. variable
• “kde”, “gnome”
76
77. X11
• Test on both KDE and GNOME
– Different shortcuts
– Different themes
– Window behavior
77
79. Windows tips
• QSettings uses the windows registry
– you can also use QSettings to read system settings
QSettings settings("HKEY_CURRENT_USER … ExplorerAdvanced”, QSettings::NativeFormat);
bool result = settings.value("EnableBalloonTips”, true).toBool();
79
80. Windows tips
• Try explorer style
http://labs.trolltech.com/blogs/2007/06/08/explorer-style-toolbars/
80
82. Windows tips
• Enable blur behind on Vista or windows 7
– No API in Qt for this yet
– However you can use the windows API directly if you
set WA_TranslucentBackground and
WA_NoSystemBackground on the widget!
82