source: trunk/doc/src/examples/shapedclock.qdoc@ 1168

Last change on this file since 1168 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: 6.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/shapedclock
30 \title Shaped Clock Example
31
32 The Shaped Clock example shows how to apply a widget mask to a top-level
33 widget to produce a shaped window.
34
35 \image shapedclock-example.png
36
37 Widget masks are used to customize the shapes of top-level widgets by restricting
38 the available area for painting. On some window systems, setting certain window flags
39 will cause the window decoration (title bar, window frame, buttons) to be disabled,
40 allowing specially-shaped windows to be created. In this example, we use this feature
41 to create a circular window containing an analog clock.
42
43 Since this example's window does not provide a \gui File menu or a close
44 button, we provide a context menu with an \gui Exit entry so that the example
45 can be closed. Click the right mouse button over the window to open this menu.
46
47 \section1 ShapedClock Class Definition
48
49 The \c ShapedClock class is based on the \c AnalogClock class defined in the
50 \l{Analog Clock Example}{Analog Clock} example. The whole class definition is
51 presented below:
52
53 \snippet examples/widgets/shapedclock/shapedclock.h 0
54
55 The \l{QWidget::paintEvent()}{paintEvent()} implementation is the same as that found
56 in the \c AnalogClock class. We implement \l{QWidget::sizeHint()}{sizeHint()}
57 so that we don't have to resize the widget explicitly. We also provide an event
58 handler for resize events. This allows us to update the mask if the clock is resized.
59
60 Since the window containing the clock widget will have no title bar, we provide
61 implementations for \l{QWidget::mouseMoveEvent()}{mouseMoveEvent()} and
62 \l{QWidget::mousePressEvent()}{mousePressEvent()} to allow the clock to be dragged
63 around the screen. The \c dragPosition variable lets us keep track of where the user
64 last clicked on the widget.
65
66 \section1 ShapedClock Class Implementation
67
68 The \c ShapedClock constructor performs many of the same tasks as the \c AnalogClock
69 constructor. We set up a timer and connect it to the widget's update() slot:
70
71 \snippet examples/widgets/shapedclock/shapedclock.cpp 0
72
73 We inform the window manager that the widget is not to be decorated with a window
74 frame by setting the Qt::FramelessWindowHint flag on the widget. As a result, we need
75 to provide a way for the user to move the clock around the screen.
76
77 Mouse button events are delivered to the \c mousePressEvent() handler:
78
79 \snippet examples/widgets/shapedclock/shapedclock.cpp 1
80
81 If the left mouse button is pressed over the widget, we record the displacement in
82 global (screen) coordinates between the top-left position of the widget's frame (even
83 when hidden) and the point where the mouse click occurred. This displacement will be
84 used if the user moves the mouse while holding down the left button. Since we acted
85 on the event, we accept it by calling its \l{QEvent::accept()}{accept()} function.
86
87 \image shapedclock-dragging.png
88
89 The \c mouseMoveEvent() handler is called if the mouse is moved over the widget.
90
91 \snippet examples/widgets/shapedclock/shapedclock.cpp 2
92
93 If the left button is held down while the mouse is moved, the top-left corner of the
94 widget is moved to the point given by subtracting the \c dragPosition from the current
95 cursor position in global coordinates. If we drag the widget, we also accept the event.
96
97 The \c paintEvent() function is given for completeness. See the
98 \l{Analog Clock Example}{Analog Clock} example for a description of the process used
99 to render the clock.
100
101 \snippet examples/widgets/shapedclock/shapedclock.cpp 3
102
103 In the \c resizeEvent() handler, we re-use some of the code from the \c paintEvent()
104 to determine the region of the widget that is visible to the user:
105
106 \snippet examples/widgets/shapedclock/shapedclock.cpp 4
107
108 Since the clock face is a circle drawn in the center of the widget, this is the region
109 we use as the mask.
110
111 Although the lack of a window frame may make it difficult for the user to resize the
112 widget on some platforms, it will not necessarily be impossible. The \c resizeEvent()
113 function ensures that the widget mask will always be updated if the widget's dimensions
114 change, and additionally ensures that it will be set up correctly when the widget is
115 first displayed.
116
117 Finally, we implement the \c sizeHint() for the widget so that it is given a reasonable
118 default size when it is first shown:
119
120 \snippet examples/widgets/shapedclock/shapedclock.cpp 5
121
122 \section1 Notes on Widget Masks
123
124 Since QRegion allows arbitrarily complex regions to be created, widget masks can be
125 made to suit the most unconventionally-shaped windows, and even allow widgets to be
126 displayed with holes in them.
127
128 Widget masks can also be constructed by using the contents of pixmap to define the
129 opaque part of the widget. For a pixmap with an alpha channel, a suitable mask can be
130 obtained with QPixmap::mask().
131*/
Note: See TracBrowser for help on using the repository browser.