How to Make Your Qt App Look Native

84
Agenda Introduction Styles Style sheets Dialogs Cross platform tips Platform specific tips 1

description

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

Transcript of How to Make Your Qt App Look Native

Page 1: How to Make Your Qt App Look Native

Agenda

•  Introduction

•  Styles

•  Style sheets

•  Dialogs

•  Cross platform tips

•  Platform specific tips

1

Page 2: How to Make Your Qt App Look Native

Who am I?

•  Jens Bache-Wiig

–  Qt developer since 2005

–  Works on look and feel

2

Page 3: How to Make Your Qt App Look Native

3

Page 4: How to Make Your Qt App Look Native

Introduction

“Media reviews of your product will be more

positive…”

Apple Interface Guidelines

4

Page 5: How to Make Your Qt App Look Native

Introduction

•  Platform guidelines

–  Windows User Experience Interaction Guidelines

–  Apple Human Interface Guidelines

–  KDE User Interface Guidelines

–  GNOME HIG

5

Page 6: How to Make Your Qt App Look Native

QStyle

6

Page 7: How to Make Your Qt App Look Native

QStyle

•  Appearance

•  Size and shape

•  Any platform specific behavior

7

Page 8: How to Make Your Qt App Look Native

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

Page 9: How to Make Your Qt App Look Native

9

Page 10: How to Make Your Qt App Look Native

10

Page 11: How to Make Your Qt App Look Native

QStyle

11

Page 12: How to Make Your Qt App Look Native

QStyle

•  What if I need to modify a style?

12

Page 13: How to Make Your Qt App Look Native

QStyle

•  Don’t subclass it!

•  Use a proxy style

13

Page 14: How to Make Your Qt App Look Native

QStyle

•  QProxyStyle introduced in 4.6

–  Makes it easy to customize the platform style

–  Without breaking it

14

Page 15: How to Make Your Qt App Look Native

QStyle

15

Page 16: How to Make Your Qt App Look Native

Agenda

•  Introduction

•  Styles

•  Style sheets

•  Dialogs

•  Cross platform tips

•  Platform specific tips

16

Page 17: How to Make Your Qt App Look Native

Style Sheets

•  Use carefully

•  Tends to break look and feel

•  Try to blend with system palette

–  Use transparency

–  Use palette colors

17

Page 18: How to Make Your Qt App Look Native

Style Sheets

18

Page 19: How to Make Your Qt App Look Native

Style Sheets

Avoid hardcoding colors QString css = "QLabel { color:palette(highlight); }”;

If you need completely custom colors QColor color(255, 0, 0);

QString css = QString("QLabel { color: %1; }").arg(color.name());

19

Page 20: How to Make Your Qt App Look Native

Style sheets

•  How can I make a segmented button?

20

Page 21: How to Make Your Qt App Look Native

Style sheets

21

Page 22: How to Make Your Qt App Look Native

Style sheets

22

Page 23: How to Make Your Qt App Look Native

Style sheets

•  Attach a style sheet to a particular style: QToolButton[style=QMacStyle] {

border-image: url(:/button_mac.png);

}

QToolButton[style=QWindowsVistaStyle] {

border-image: url(:/button_vista.png);

}

23

Page 24: How to Make Your Qt App Look Native

Style sheets

24

Page 25: How to Make Your Qt App Look Native

Agenda

•  Introduction

•  Styles

•  Style sheets

•  Dialogs

•  Cross platform tips

•  Platform specific tips

25

Page 26: How to Make Your Qt App Look Native

Dialogs

Windows

26

Mac

KDE GNOME

Page 27: How to Make Your Qt App Look Native

Dialogs

QDialogButtonBox

- Manages order, layout, icon and text

QDialogButtonBox box(QDialogButtonBox::Save |

QDalogButtonBox::Discard |

QDialogButtonBox::Cancel);

27

Page 28: How to Make Your Qt App Look Native

Dialogs

•  Custom buttons are marked with a role

–  Determines positioning

–  Reject, accept, help … QDialogButtonBox box;

box.addButton(myButton, QDialogButtonBox::AcceptRole);

28

Page 29: How to Make Your Qt App Look Native

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

Page 30: How to Make Your Qt App Look Native

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

Page 31: How to Make Your Qt App Look Native

Dialogs

•  What happens when you open a modal dialog?

31

Page 32: How to Make Your Qt App Look Native

Dialogs

•  QDialog::show() - non modal

•  QDialog::exec() - modal

•  QDialog::open() – window modal

32

Page 33: How to Make Your Qt App Look Native

Dialogs

Using QDialog::open() :

33

Page 34: How to Make Your Qt App Look Native

Dialogs

34

Page 35: How to Make Your Qt App Look Native

Dialogs

35

Page 36: How to Make Your Qt App Look Native

Dialogs

36

Page 37: How to Make Your Qt App Look Native

Agenda

•  Introduction

•  Styles

•  Style sheets

•  Dialogs

•  Cross platform tips

•  Platform specific tips

37

Page 38: How to Make Your Qt App Look Native

Cross platform tips

How do you tell the user that the current document was

modified?

38

Page 39: How to Make Your Qt App Look Native

Cross platform tips

•  When showing a file location in the title bar

setWindowModified(true);

setWindowFilePath("untitled.txt");

39

Page 40: How to Make Your Qt App Look Native

Cross platform tips

40

Page 41: How to Make Your Qt App Look Native

Cross platform tips

How can your application ask for attention?

41

Page 42: How to Make Your Qt App Look Native

Cross platform tips

QApplication::alert(widget, msec = 0);

•  Bouncing dock icon on Mac

•  Flashing taskbar entry on Windows

42

Page 43: How to Make Your Qt App Look Native

Cross platform tips

•  QSystemTrayIcon

-use b/w icon on mac

43

Page 44: How to Make Your Qt App Look Native

Cross platform tips

Where do you store your documents?

44

Page 45: How to Make Your Qt App Look Native

Cross platform tips

•  QDesktopServices::storageLocation()

–  gives you default system directories such as

Desktop, Music, Pictures, Applications, Temp and

Cache

45

Page 46: How to Make Your Qt App Look Native

Cross platform tips

How can you open an e-mail using your standard e-mail

application?

46

Page 47: How to Make Your Qt App Look Native

Cross platform tips

•  QDesktopServices::openUrl(const QUrl &url)

–  Launches the provided URL using the default system

application

openUrl(“mailto:[email protected]”);

openUrl(“http://qt.nokia.com”);

openUrl(QUrl::fromLocalFile(…));

47

Page 48: How to Make Your Qt App Look Native

Cross platform tips

Which shortcut do I use for “find” in a document?

48

Page 49: How to Make Your Qt App Look Native

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

Page 50: How to Make Your Qt App Look Native

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

Page 51: How to Make Your Qt App Look Native

Cross platform tips

•  Use a consistent icon theme

•  Lots of free themes available online

–  Oxygen, Tango to mention a few

51

http://tango.freedesktop.org http://www.oxygen-icons.org

Page 52: How to Make Your Qt App Look Native

Cross platform tips

•  Icon theme support in 4.6

QIcon::fromTheme(“document-edit”);

QIcon::fromTheme(“document-edit”,

QIcon(“document.png”));

52

For previous versions of Qt: http://code.google.com/p/qticonloader/

Page 53: How to Make Your Qt App Look Native

Cross platform tips

53

Page 54: How to Make Your Qt App Look Native

Cross platform tips

54

Page 55: How to Make Your Qt App Look Native

Cross platform tips

•  Give your QAction priority –  Introduced in 4.6

–  low, normal and high priority

QAction::setPriority(QAction::Priority)

55

Page 56: How to Make Your Qt App Look Native

Cross platform tips

56

Page 57: How to Make Your Qt App Look Native

Cross platform tips

57

Page 58: How to Make Your Qt App Look Native

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

Page 59: How to Make Your Qt App Look Native

Cross platform tips - Dialogs

•  Preferences on GNOME/Mac

–  Applies immediately

•  Preferences on Windows/KDE

–  Apply/Cancel

59

Page 60: How to Make Your Qt App Look Native

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

Page 61: How to Make Your Qt App Look Native

Go native!

•  Ifdef is evil but sometimes useful…

–  Q_WS_WIN

–  Q_WS_MAC

–  Q_WS_X11

61

Page 62: How to Make Your Qt App Look Native

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

Page 63: How to Make Your Qt App Look Native

Agenda

•  Introduction

•  Styles

•  Style sheets

•  Dialogs

•  Cross platform tips

•  Platform specific tips

63

Page 64: How to Make Your Qt App Look Native

Platform specific tips

64

Page 65: How to Make Your Qt App Look Native

Mac

–  MacMainWindow demo

65

Page 66: How to Make Your Qt App Look Native

Mac

•  Icons

–  Use a high-resolution application icon

–  Use a b/w system tray icon

66

Page 67: How to Make Your Qt App Look Native

Mac

•  QMenuBar can stand on it’s own!

–  Create it without a parent

–  The first menu bar created will be the default menu bar

67

Page 68: How to Make Your Qt App Look Native

Mac

QMainWindow::setUnifiedTitleAndToolBarOnMac()

68

Page 69: How to Make Your Qt App Look Native

Mac

•  Why not allways set it?

–  Not movable

–  No breaks are respected

–  Custom widgets hidden when small

–  Toolbar hidden in fullscreen

69

Page 70: How to Make Your Qt App Look Native

Mac

•  Unified toolbar breakage….

70

Page 71: How to Make Your Qt App Look Native

Mac – Doc menu

71

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);

Page 72: How to Make Your Qt App Look Native

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

Page 73: How to Make Your Qt App Look Native

X11

73

Page 74: How to Make Your Qt App Look Native

X11

•  Follow freedesktop.org standards if possible

–  Menu specs

–  Icon themes

–  Autostart

–  Bookmarks

–  .desktop file

74

Page 75: How to Make Your Qt App Look Native

X11

•  Make a desktop file

–  standards.freedesktop.org/desktop-entry-spec/

–  Simple configuration file containing

•  Application Icon

•  Menu Entry

•  Registered mime types

•  …

75

Page 76: How to Make Your Qt App Look Native

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

Page 77: How to Make Your Qt App Look Native

X11

•  Test on both KDE and GNOME

–  Different shortcuts

–  Different themes

–  Window behavior

77

Page 78: How to Make Your Qt App Look Native

78

Page 79: How to Make Your Qt App Look Native

Windows tips

•  QSettings uses the windows registry

–  you can also use QSettings to read system settings

QSettings settings("HKEY_CURRENT_USER\\ … Explorer\\Advanced”, QSettings::NativeFormat);

bool result = settings.value("EnableBalloonTips”, true).toBool();

79

Page 80: How to Make Your Qt App Look Native

Windows tips

•  Try explorer style

80

http://labs.trolltech.com/blogs/2007/06/08/explorer-style-toolbars/

Page 81: How to Make Your Qt App Look Native

Windows tips

•  QtDotNetStyle

–  Free solution

81

Page 82: How to Make Your Qt App Look Native

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

Page 83: How to Make Your Qt App Look Native

Windows

83

Page 84: How to Make Your Qt App Look Native

Conclusion

84