source: trunk/doc/src/examples/shapedclock.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: 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