Drawing shapes

The PySide.QtGui.QPainter class performs low-level painting on widgets and other paint devices. The QPainter class provides all the functions to draw simple lines to more complex shapes. This class also provides settings to render 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, such as pen style and brush style, and then the draw function is called. The QPainter.isActive() function indicates whether 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 as follows:

  • Pen: This is used to draw lines, shapes, and outlines. It takes various settings to draw that include color, width, line style, and so on
  • Brush: This is used to pattern fill geometric shapes. The various settings that a brush can take include color, style, texture, gradient, and so on
  • Font: This is mainly used to draw Unicode text. The font settings include font style, font family, and point size

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:

Constant

Description

drawPoint()

This draws a single point at the given position

drawText()

This draws the given text within the defined rectangle

drawLine()

This draws a line between two pairs of points

drawRect()

This draws a rectangle

drawRoundedRect()

This draws a rectangle with rounded edges or corners

drawEllipse()

This draws an ellipse that is defined by the given rectangle

drawArc()

This draws an arc that is defined by the given rectangle

drawPie()

This draws a pie that is defined by the given rectangle

drawChord()

This draws a chord that is defined by the given rectangle

drawPolyline()

This draws a polyline that is defined by the given points

drawPolygon()

This draws a polygon that is defined by the given points

drawConvexPolygon()

This draws a convex polygon that is defined by the given polygon

drawImage()

This draws the given image inside the given rectangle

drawPath()

This draws the given painter path that is defined by the QPainterPath

drawPicture()

This draws the given picture

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 drawing functions, and hence, we should look at 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 this book's site. Here, we just display 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 the usage of other built-in widgets, such as combobox, that we will discuss in the next chapter. As of now, it is sufficient if you can understand the reimplementation of the 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 will produce a window, as displayed in the following screenshot. You can select the choice of your drawing from the comboboxes, and it will be painted in the application.

Drawing shapes
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset