Drawing

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:

  • Pen: The pen is used for drawing lines and shapes outlines. It takes various settings for drawing that include color, width, line style, and so on.
  • Brush: The brush is used for pattern filling of geometric shapes. The various settings that a brush can take include color, style, texture, gradient, and so on.
  • Font: The font is mainly used for drawing 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:

Function

Description

drawPoint()

Draws a single point at the given position

drawText()

Draws the given text within the defined rectangle

drawLine()

Draws a line between two point pairs

drawRect()

Draws a rectangle by the given rectangle

drawRoundedRect()

Draws a rectangle with rounded edges or corners

drawEllipse()

Draws an ellipse defined by the given rectangle

drawArc()

Draws an arc defined by the given rectangle

drawPie()

Draws a pie defined by the given rectangle

drawChord()

Draws a chord defined by the given rectangle

drawPolyline()

Draws a polyline defined by the given points

drawPolygon()

Draws a polygon defined by the given points

drawConvexPolygon()

Draws a convex polygon defined by the given polygon

drawImage()

Draws the given Image into the given rectangle

drawPath()

Draws the given painter path defined by the QPainterPath

drawPicture()

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 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:

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

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