| 225 | |
| 226 | |
| 227 | class PaintArea(QWidget): |
| 228 | def __init__(self): |
| 229 | super(PaintArea, self).__init__() |
| 230 | self.Shape = ["Line", "Rectangle", 'Rounded Rectangle', "Ellipse", "Pie", 'Chord', |
| 231 | "Path", "Polygon", "Polyline", "Arc", "Points", "Text", "Pixmap"] |
| 232 | self.setPalette(QPalette(Qt.white)) |
| 233 | self.setAutoFillBackground(True) |
| 234 | self.setMinimumSize(500, 500) |
| 235 | self.pen = QPen() |
| 236 | self.brush = QBrush() |
| 237 | |
| 238 | def setShape(self, s): |
| 239 | self.shape = s |
| 240 | self.update() |
| 241 | |
| 242 | def setPen(self, p): |
| 243 | self.pen = p |
| 244 | self.update() |
| 245 | |
| 246 | def setBrush(self, b): |
| 247 | self.brush = b |
| 248 | self.update() |
| 249 | |
| 250 | def paintEvent(self, QPaintEvent): |
| 251 | p = QPainter(self) |
| 252 | p.setPen(self.pen) |
| 253 | p.setBrush(self.brush) |
| 254 | |
| 255 | rect = QRect(50, 100, 300, 200) |
| 256 | points = [QPoint(150, 100), QPoint(300, 150), QPoint(350, 250), QPoint(100, 300)] |
| 257 | startAngle = 30 * 16 |
| 258 | spanAngle = 120 * 16 |
| 259 | |
| 260 | if self.shape == "Line": |
| 261 | p.drawLine(rect.topLeft(), rect.bottomRight()) |
| 262 | elif self.shape == "Rectangle": |
| 263 | p.drawRect(rect) |
| 264 | elif self.shape == 'Rounded Rectangle': |
| 265 | p.drawRoundedRect(rect, 25, 25, Qt.RelativeSize) |
| 266 | elif self.shape == "Ellipse": |
| 267 | p.drawEllipse(rect) |
| 268 | elif self.shape == "Polygon": |
| 269 | p.drawPolygon(QPolygon(points), Qt.WindingFill) |
| 270 | elif self.shape == "Polyline": |
| 271 | p.drawPolyline(QPolygon(points)) |
| 272 | elif self.shape == "Points": |
| 273 | p.drawPoints(QPolygon(points)) |
| 274 | elif self.shape == "Pie": |
| 275 | p.drawPie(rect, startAngle, spanAngle) |
| 276 | elif self.shape == "Arc": |
| 277 | p.drawArc(rect, startAngle, spanAngle) |
| 278 | elif self.shape == "Chord": |
| 279 | p.drawChord(rect, startAngle, spanAngle) |
| 280 | elif self.shape == "Path": |
| 281 | path = QPainterPath() |
| 282 | path.addRect(150, 150, 100, 100) |
| 283 | path.moveTo(100, 100) |
| 284 | path.cubicTo(300, 100, 200, 200, 300, 300) |