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 painting/basicdrawing
|
---|
30 | \title Basic Drawing Example
|
---|
31 |
|
---|
32 | The Basic Drawing example shows how to display basic graphics
|
---|
33 | primitives in a variety of styles using the QPainter class.
|
---|
34 |
|
---|
35 | QPainter performs low-level painting on widgets and other paint
|
---|
36 | devices. The class can draw everything from simple lines to
|
---|
37 | complex shapes like pies and chords. It can also draw aligned text
|
---|
38 | and pixmaps. Normally, it draws in a "natural" coordinate system,
|
---|
39 | but it can in addition do view and world transformation.
|
---|
40 |
|
---|
41 | \image basicdrawing-example.png
|
---|
42 |
|
---|
43 | The example provides a render area, displaying the currently
|
---|
44 | active shape, and lets the user manipulate the rendered shape and
|
---|
45 | its appearance using the QPainter parameters: The user can change
|
---|
46 | the active shape (\gui Shape), and modify the QPainter's pen (\gui
|
---|
47 | {Pen Width}, \gui {Pen Style}, \gui {Pen Cap}, \gui {Pen Join}),
|
---|
48 | brush (\gui {Brush Style}) and render hints (\gui
|
---|
49 | Antialiasing). In addition the user can rotate a shape (\gui
|
---|
50 | Transformations); behind the scenes we use QPainter's ability to
|
---|
51 | manipulate the coordinate system to perform the rotation.
|
---|
52 |
|
---|
53 | The Basic Drawing example consists of two classes:
|
---|
54 |
|
---|
55 | \list
|
---|
56 | \o \c RenderArea is a custom widget that renders multiple
|
---|
57 | copies of the currently active shape.
|
---|
58 | \o \c Window is the application's main window displaying a
|
---|
59 | \c RenderArea widget in addition to several parameter widgets.
|
---|
60 | \endlist
|
---|
61 |
|
---|
62 | First we will review the \c Window class, then we will take a
|
---|
63 | look at the \c RenderArea class.
|
---|
64 |
|
---|
65 | \section1 Window Class Definition
|
---|
66 |
|
---|
67 | The Window class inherits QWidget, and is the application's main
|
---|
68 | window displaying a \c RenderArea widget in addition to several
|
---|
69 | parameter widgets.
|
---|
70 |
|
---|
71 | \snippet examples/painting/basicdrawing/window.h 0
|
---|
72 |
|
---|
73 | We declare the various widgets, and three private slots updating
|
---|
74 | the \c RenderArea widget: The \c shapeChanged() slot updates the
|
---|
75 | \c RenderArea widget when the user changes the currently active
|
---|
76 | shape. We call the \c penChanged() slot when either of the
|
---|
77 | QPainter's pen parameters changes. And the \c brushChanged() slot
|
---|
78 | updates the \c RenderArea widget when the user changes the
|
---|
79 | painter's brush style.
|
---|
80 |
|
---|
81 | \section1 Window Class Implementation
|
---|
82 |
|
---|
83 | In the constructor we create and initialize the various widgets
|
---|
84 | appearing in the main application window.
|
---|
85 |
|
---|
86 | \snippet examples/painting/basicdrawing/window.cpp 1
|
---|
87 |
|
---|
88 | First we create the \c RenderArea widget that will render the
|
---|
89 | currently active shape. Then we create the \gui Shape combobox,
|
---|
90 | and add the associated items (i.e. the different shapes a QPainter
|
---|
91 | can draw).
|
---|
92 |
|
---|
93 | \snippet examples/painting/basicdrawing/window.cpp 2
|
---|
94 |
|
---|
95 | QPainter's pen is a QPen object; the QPen class defines how a
|
---|
96 | painter should draw lines and outlines of shapes. A pen has
|
---|
97 | several properties: Width, style, cap and join.
|
---|
98 |
|
---|
99 | A pen's width can be \e zero or greater, but the most common width
|
---|
100 | is zero. Note that this doesn't mean 0 pixels, but implies that
|
---|
101 | the shape is drawn as smoothly as possible although perhaps not
|
---|
102 | mathematically correct.
|
---|
103 |
|
---|
104 | We create a QSpinBox for the \gui {Pen Width} parameter.
|
---|
105 |
|
---|
106 | \snippet examples/painting/basicdrawing/window.cpp 3
|
---|
107 |
|
---|
108 | The pen style defines the line type. The default style is solid
|
---|
109 | (Qt::SolidLine). Setting the style to none (Qt::NoPen) tells the
|
---|
110 | painter to not draw lines or outlines. The pen cap defines how
|
---|
111 | the end points of lines are drawn. And the pen join defines how
|
---|
112 | two lines join when multiple connected lines are drawn. The cap
|
---|
|
---|