source: trunk/doc/src/examples/tooltips.qdoc

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 17.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at [email protected].
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \example widgets/tooltips
30 \title Tool Tips Example
31
32 The Tool Tips example shows how to provide static and dynamic tool
33 tips for an application's widgets.
34
35 The simplest and most common way to set a widget's tool tip is by
36 calling its QWidget::setToolTip() function (static tool
37 tips). Then the tool tip is shown whenever the cursor points at
38 the widget. We show how to do this with our application's tool
39 buttons. But it is also possible to show different tool tips
40 depending on the cursor's position (dynamic tooltips). This
41 approach uses mouse tracking and event handling to determine what
42 widgets are located under the cursor at any point in time, and
43 displays their tool tips. The tool tips for the shape items in our
44 application are implemented using the latter approach.
45
46 \image tooltips-example.png
47
48 With the \c Tooltips application the user can create new shape
49 items with the provided tool buttons, and move the items around
50 using the mouse. Tooltips are provided whenever the cursor is
51 pointing to a shape item or one of the buttons.
52
53 The Tooltips example consists of two classes:
54
55 \list
56 \o \c ShapeItem is a custom widget representing one single shape item.
57 \o \c SortingBox inherits from QWidget and is the application's main
58 widget.
59 \endlist
60
61 First we will review the \c SortingBox class, then we will take a
62 look at the \c ShapeItem class.
63
64 \section1 SortingBox Class Definition
65
66 \snippet examples/widgets/tooltips/sortingbox.h 0
67
68 The \c SortingBox class inherits QWidget, and it is the Tooltips
69 application's main widget. We reimplement several of the event
70 handlers.
71
72 The \c event() function provides tooltips, the \c resize()
73 function makes sure the application appears consistently when the
74 user resizes the main widget, and the \c paintEvent() function
75 displays the shape items within the \c SortingBox widget. The
76 mouse event handlers are reimplemented to make the user able to
77 move the items around.
78
79 In addition we need three private slots to make the user able to
80 create new shape items.
81
82 \snippet examples/widgets/tooltips/sortingbox.h 1
83
84 We also create several private functions: We use the \c
85 initialItemPosition(), \c initialItemColor() and \c
86 createToolButton() functions when we are constructing the widget,
87 and we use the \c updateButtonGeometry() function whenever the
88 user is resizing the application's main widget.
89
90 The \c itemAt() function determines if there is a shape item at a
91 particular position, and the \c moveItemTo() function moves an
92 item to a new position. We use the \c createShapeItem(), \c
93 randomItemPosition() and \c randomItemColor() functions to create
94 new shape items.