PyQt - Drawing Effects



A drawing effect refers to various styles that are used to create a visual impact on drawing. It could involve gradient, shading, image, or other method that enhance the appearance of artwork over texture.

QPixmap Class

The QPixmap class in Qt refers to off_screen image representation which handles the image data. It can be used as a QPaintdevice object or we can say, loaded into another widget.

Following key points for QPixmap class over gradient and image

  • We can fill the color of pixmap with a specific color using fill(color).
  • Create QLinearGradient to define the gradient effect.
  • Insert correct path to the image file and handle the error.
  • Ensure that the application must enter the Qt event loop to handle the events.

Drawing a Gradient in PyQt

A gradient is defined using transformation of color progresses from origin to end. A gradient can be achieved through layer or selection, and create a smooth transition from one layer to another. For example − In Machine Learning gradient plays a vital role in training the model.

color transition

The QGradient class arrangement of QBrush to specify gradient fills. In addition, Qpen uses all its convenience classes and object to set the gradient effect on container.

Example

Following example illustrate the code of drawing gradient.

from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtGui import QPainter, QPen, QBrush, QLinearGradient

class MyWidget(QtWidgets.QWidget):
   def paintEvent(self, event):
      painter = QPainter(self)
      painter.setPen(QPen(QtCore.Qt.green, 5, QtCore.Qt.SolidLine))
      grad = QLinearGradient(90, 50, 30, 10)
      painter.setBrush(QBrush(grad))
      painter.drawRect(10, 10, 200, 200)

app = QtWidgets.QApplication([])
widget = MyWidget()
widget.show()
app.exec_()

Output

The above code produces the following result −

linear gradient

Drawing an image in PyQt

Drawing on images is very simple in PyQt. The drawing of any shape over an image elevates the new painting as an output result. Below the figure drawn rectangle shape over an image −

drawing image

Following step to perform the task of drawing geometric figure over an image −

Step 1 − Start creating object with QPainter.

Step 2 − Set the path of image using QPixmap() method.

Step 3 − Display the image window which we used for the method drawPixmap().

Step 4 − Set the line color and style for rectangle using setPen().

Step 5 − Using drawRect() draw a rectangle and it will drawn up on the middle of the image.

Example

In this example, we demonstrate the code to draw the geometrical shapes over images using PyQt.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class Example(QWidget):
   def __init__(self):
      super().__init__()
      self.setGeometry(30, 30, 500, 300)
   def paintEvent(self, event):
      painter = QPainter(self)
      pixmap = QPixmap("C:/Users/Lenovo/Downloads/tutorialspoint.png")
      painter.drawPixmap(self.rect(), pixmap)
      pen = QPen(Qt.gray, 3)
      painter.setPen(pen)
      painter.drawRect(40, 40, 400, 200)
if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = Example()
   ex.show()
   sys.exit(app.exec_())

Output

On executing the code, we observed that −

draw image
Advertisements