1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: window.cpp Example File (painting/basicdrawing/window.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">window.cpp Example File<br /><small><small>painting/basicdrawing/window.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be distributed under the terms of the Q Public License
27
** as defined by Trolltech AS of Norway and appearing in the file
28
** LICENSE.QPL included in the packaging of this file.
30
** This file may be distributed and/or modified under the terms of the
31
** GNU General Public License version 2 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file.
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
36
** information about Qt Commercial License Agreements.
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
40
** Contact info@trolltech.com if any conditions of this licensing are
43
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
44
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
****************************************************************************/
48
#include <QtGui>
50
#include "renderarea.h"
51
#include "window.h"
53
const int IdRole = Qt::UserRole;
57
renderArea = new RenderArea;
59
shapeComboBox = new QComboBox;
60
shapeComboBox->addItem(tr("Rectangle"), RenderArea::Rect);
61
shapeComboBox->addItem(tr("Round Rectangle"), RenderArea::RoundRect);
62
shapeComboBox->addItem(tr("Ellipse"), RenderArea::Ellipse);
63
shapeComboBox->addItem(tr("Pie"), RenderArea::Pie);
64
shapeComboBox->addItem(tr("Chord"), RenderArea::Chord);
65
shapeComboBox->addItem(tr("Polygon"), RenderArea::Polygon);
66
shapeComboBox->addItem(tr("Path"), RenderArea::Path);
67
shapeComboBox->addItem(tr("Line"), RenderArea::Line);
68
shapeComboBox->addItem(tr("Polyline"), RenderArea::Polyline);
69
shapeComboBox->addItem(tr("Arc"), RenderArea::Arc);
70
shapeComboBox->addItem(tr("Points"), RenderArea::Points);
71
shapeComboBox->addItem(tr("Text"), RenderArea::Text);
72
shapeComboBox->addItem(tr("Pixmap"), RenderArea::Pixmap);
74
shapeLabel = new QLabel(tr("&Shape:"));
75
shapeLabel->setBuddy(shapeComboBox);
77
penWidthSpinBox = new QSpinBox;
78
penWidthSpinBox->setRange(0, 20);
80
penWidthLabel = new QLabel(tr("Pen &Width:"));
81
penWidthLabel->setBuddy(penWidthSpinBox);
83
penStyleComboBox = new QComboBox;
84
penStyleComboBox->addItem(tr("Solid"), Qt::SolidLine);
85
penStyleComboBox->addItem(tr("Dash"), Qt::DashLine);
86
penStyleComboBox->addItem(tr("Dot"), Qt::DotLine);
87
penStyleComboBox->addItem(tr("Dash Dot"), Qt::DashDotLine);
88
penStyleComboBox->addItem(tr("Dash Dot Dot"), Qt::DashDotDotLine);
89
penStyleComboBox->addItem(tr("None"), Qt::NoPen);
91
penStyleLabel = new QLabel(tr("&Pen Style:"));
92
penStyleLabel->setBuddy(penStyleComboBox);
94
penCapComboBox = new QComboBox;
95
penCapComboBox->addItem(tr("Flat"), Qt::FlatCap);
96
penCapComboBox->addItem(tr("Square"), Qt::SquareCap);
97
penCapComboBox->addItem(tr("Round"), Qt::RoundCap);
99
penCapLabel = new QLabel(tr("Pen &Cap:"));
100
penCapLabel->setBuddy(penCapComboBox);
102
penJoinComboBox = new QComboBox;
103
penJoinComboBox->addItem(tr("Miter"), Qt::MiterJoin);
104
penJoinComboBox->addItem(tr("Bevel"), Qt::BevelJoin);
105
penJoinComboBox->addItem(tr("Round"), Qt::RoundJoin);
107
penJoinLabel = new QLabel(tr("Pen &Join:"));
108
penJoinLabel->setBuddy(penJoinComboBox);
110
brushStyleComboBox = new QComboBox;
111
brushStyleComboBox->addItem(tr("Linear Gradient"),
112
Qt::LinearGradientPattern);
113
brushStyleComboBox->addItem(tr("Radial Gradient"),
114
Qt::RadialGradientPattern);
115
brushStyleComboBox->addItem(tr("Conical Gradient"),
116
Qt::ConicalGradientPattern);
117
brushStyleComboBox->addItem(tr("Texture"), Qt::TexturePattern);
118
brushStyleComboBox->addItem(tr("Solid"), Qt::SolidPattern);
119
brushStyleComboBox->addItem(tr("Horizontal"), Qt::HorPattern);
120
brushStyleComboBox->addItem(tr("Vertical"), Qt::VerPattern);
121
brushStyleComboBox->addItem(tr("Cross"), Qt::CrossPattern);
122
brushStyleComboBox->addItem(tr("Backward Diagonal"), Qt::BDiagPattern);
123
brushStyleComboBox->addItem(tr("Forward Diagonal"), Qt::FDiagPattern);
124
brushStyleComboBox->addItem(tr("Diagonal Cross"), Qt::DiagCrossPattern);
125
brushStyleComboBox->addItem(tr("Dense 1"), Qt::Dense1Pattern);
126
brushStyleComboBox->addItem(tr("Dense 2"), Qt::Dense2Pattern);
127
brushStyleComboBox->addItem(tr("Dense 3"), Qt::Dense3Pattern);
128
brushStyleComboBox->addItem(tr("Dense 4"), Qt::Dense4Pattern);
129
brushStyleComboBox->addItem(tr("Dense 5"), Qt::Dense5Pattern);
130
brushStyleComboBox->addItem(tr("Dense 6"), Qt::Dense6Pattern);
131
brushStyleComboBox->addItem(tr("Dense 7"), Qt::Dense7Pattern);
132
brushStyleComboBox->addItem(tr("None"), Qt::NoBrush);
134
brushStyleLabel = new QLabel(tr("&Brush Style:"));
135
brushStyleLabel->setBuddy(brushStyleComboBox);
137
antialiasingCheckBox = new QCheckBox(tr("&Antialiasing"));
138
transformationsCheckBox = new QCheckBox(tr("&Transformations"));
140
connect(shapeComboBox, SIGNAL(activated(int)),
141
this, SLOT(shapeChanged()));
142
connect(penWidthSpinBox, SIGNAL(valueChanged(int)),
143
this, SLOT(penChanged()));
144
connect(penStyleComboBox, SIGNAL(activated(int)),
145
this, SLOT(penChanged()));
146
connect(penCapComboBox, SIGNAL(activated(int)),
147
this, SLOT(penChanged()));
148
connect(penJoinComboBox, SIGNAL(activated(int)),
149
this, SLOT(penChanged()));
150
connect(brushStyleComboBox, SIGNAL(activated(int)),
151
this, SLOT(brushChanged()));
152
connect(antialiasingCheckBox, SIGNAL(toggled(bool)),
153
renderArea, SLOT(setAntialiased(bool)));
154
connect(transformationsCheckBox, SIGNAL(toggled(bool)),
155
renderArea, SLOT(setTransformed(bool)));
157
QHBoxLayout *checkBoxLayout = new QHBoxLayout;
158
checkBoxLayout->addWidget(antialiasingCheckBox);
159
checkBoxLayout->addWidget(transformationsCheckBox);
161
QGridLayout *mainLayout = new QGridLayout;
162
mainLayout->addWidget(renderArea, 0, 0, 1, 2);
163
mainLayout->addWidget(shapeLabel, 1, 0);
164
mainLayout->addWidget(shapeComboBox, 1, 1);
165
mainLayout->addWidget(penWidthLabel, 2, 0);
166
mainLayout->addWidget(penWidthSpinBox, 2, 1);
167
mainLayout->addWidget(penStyleLabel, 3, 0);
168
mainLayout->addWidget(penStyleComboBox, 3, 1);
169
mainLayout->addWidget(penCapLabel, 4, 0);
170
mainLayout->addWidget(penCapComboBox, 4, 1);
171
mainLayout->addWidget(penJoinLabel, 5, 0);
172
mainLayout->addWidget(penJoinComboBox, 5, 1);
173
mainLayout->addWidget(brushStyleLabel, 6, 0);
174
mainLayout->addWidget(brushStyleComboBox, 6, 1);
175
mainLayout->addLayout(checkBoxLayout, 7, 0, 1, 2);
176
setLayout(mainLayout);
182
renderArea->setAntialiased(false);
183
renderArea->setTransformed(false);
185
setWindowTitle(tr("Basic Drawing"));
188
void Window::shapeChanged()
190
RenderArea::Shape shape =
191
(RenderArea::Shape)shapeComboBox->itemData(shapeComboBox->currentIndex(), IdRole).toInt();
192
renderArea->setShape(shape);
195
void Window::penChanged()
197
int width = penWidthSpinBox->value();
199
(Qt::PenStyle)penStyleComboBox->itemData(penStyleComboBox->currentIndex(), IdRole).toInt();
200
Qt::PenCapStyle cap =
201
(Qt::PenCapStyle)penCapComboBox->itemData(penCapComboBox->currentIndex(), IdRole).toInt();
202
Qt::PenJoinStyle join =
203
(Qt::PenJoinStyle)penJoinComboBox->itemData(penJoinComboBox->currentIndex(), IdRole).toInt();
205
renderArea->setPen(QPen(Qt::blue, width, style, cap, join));
208
void Window::brushChanged()
210
Qt::BrushStyle style =
211
(Qt::BrushStyle)brushStyleComboBox->itemData(brushStyleComboBox->currentIndex(), IdRole).toInt();
213
if (style == Qt::LinearGradientPattern) {
214
QLinearGradient linearGradient(0, 0, 100, 100);
215
linearGradient.setColorAt(0.0, Qt::white);
216
linearGradient.setColorAt(0.2, Qt::green);
217
linearGradient.setColorAt(1.0, Qt::black);
218
renderArea->setBrush(linearGradient);
219
} else if (style == Qt::RadialGradientPattern) {
220
QRadialGradient radialGradient(50, 50, 50, 50, 50);
221
radialGradient.setColorAt(0.0, Qt::white);
222
radialGradient.setColorAt(0.2, Qt::green);
223
radialGradient.setColorAt(1.0, Qt::black);
224
renderArea->setBrush(radialGradient);
225
} else if (style == Qt::ConicalGradientPattern) {
226
QConicalGradient conicalGradient(50, 50, 150);
227
conicalGradient.setColorAt(0.0, Qt::white);
228
conicalGradient.setColorAt(0.2, Qt::green);
229
conicalGradient.setColorAt(1.0, Qt::black);
230
renderArea->setBrush(conicalGradient);
231
} else if (style == Qt::TexturePattern) {
232
renderArea->setBrush(QBrush(QPixmap(":/images/brick.png")));
234
renderArea->setBrush(QBrush(Qt::red, style));
237
<p /><address><hr /><div align="center">
238
<table width="100%" cellspacing="0" border="0"><tr class="address">
239
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
240
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
241
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
242
</tr></table></div></address></body>