The PySide.QtGui.QPainter
class performs low-level painting on widgets and other paint devices. The QPainter
class provides all the functions for drawing simple lines to more complex shapes. This class also provides settings for rendering quality images and supports clipping. The drawing is usually done within the widget's paintEvent()
function. The drawing functionalities are placed in between the begin()
and end()
functions of the QPainter
object. The QPainter
object is initialized with the constructor, customized with some set functions, for example, pen style and brush style, and then the draw function is called. The QPainter.isActive()
function indicates if the painter is active. The QPainter
object is activated when QPainter.begin()
is invoked and deactivated on calling QPainter.end()
.
The various drawings are performed using the QPainter's draw functions. The QPainter has three important settings that set the appearance of the drawing. They are:
The settings for these parameters can be set and modified anytime by calling the setFont()
, setBrush()
, and setFont()
on QPainter with their respective QPen, QBrush, or QFont objects.
In this section, we are going to explore the most commonly used drawing shapes. The following table will give you a gist of the available draw functions of the QPainter object:
Function |
Description |
---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
All the earlier listed functions take various arguments as their parameters for different drawing functionalities. Also, all these drawing functions use the current pen, brush, and text settings to draw the objects. This section is not enough to cover and discuss all the different types of the drawing functions and hence we would see a sample program that is self-explanatory and exhibits the different styles of the listed functions. The complete version of the basic drawing functionality code can be downloaded from the book's site. Here, we just show the contents of the paintEvent()
function with different drawing shapes. The complete code is bundled with event handling that we have discussed in the first section of this chapter and usage of other built-in widgets like Combo-Box that we would discuss in the next chapter. As of now, it is sufficient for you if you could understand the reimplementation of event handlers and drawing functions of the program:
def paintEvent(self, event): rect = QRect(10, 20, 80, 60) startAngle = 30 * 16 arcLength = 120 * 16 painter = QPainter() painter.begin(self) painter.setPen(self.pen) painter.setBrush(self.brush) if self.shape == PaintArea.Line: painter.drawLine(rect.bottomLeft(), rect.topRight()) elif self.shape == PaintArea.Points: painter.drawPoints(PaintArea.points) elif self.shape == PaintArea.Polyline: painter.drawPolyline(PaintArea.points) elif self.shape == PaintArea.Polygon: painter.drawPolygon(PaintArea.points) elif self.shape == PaintArea.Rect: painter.drawRect(rect) elif self.shape == PaintArea.RoundRect: painter.drawRoundRect(rect) elif self.shape == PaintArea.Ellipse: painter.drawEllipse(rect) elif self.shape == PaintArea.Arc: painter.drawArc(rect, startAngle, arcLength) elif self.shape == PaintArea.Chord: painter.drawChord(rect, startAngle, arcLength) elif self.shape == PaintArea.Pie: painter.drawPie(rect, startAngle, arcLength) elif self.shape == PaintArea.Path: painter.drawPath(path) elif self.shape == PaintArea.Text: painter.drawText(rect, QtCore.Qt.AlignCenter, "Basic Drawing Widget") painter.end()
This would produce a window as given in the following screenshot. You can select from the combo boxes the choice of your drawing and it would be painted in the application: