Qt Basic Drawing

Objectives

  • Learn how to draw on widgets
  • Understand the difference between QPixmap, QImage and QPicture
  • Be able to load an image from a file

QPainter

  • QPainter can draw geometric shapes (points, lines, rectangles, ellipses, arcs, chords, pie segments, polygons, and B├ęzier curves) and also pixmaps, images, and text.
  • QPainter can be used to draw on a "paint device", like a QWidget, a QPixmap or a QImage.
  • The way the drawing is performed is influenced by QPainter's settings. The three main settings are:
    • Pen: Used for drawing lines and shape outlines.
    • Brush: Pattern used for filling geometric shapes.
    • Font: For drawing text.
  • These settings can be modified by calling setPen(), setBrush(), and setFont() with a QPen, QBrush, or QFont object.

Qt QPainter Example

Difference between QPixmap, QImage and QPicture

  • Qt provides four classes for handling image data: QImage, QPixmap and QPicture.
  • QImage provides a hardware-independent image representation. It is is designed and optimized for I/O, and for direct pixel access and manipulation
  • QPixmap is designed and optimized for showing images on screen.
  • QPicture class is a paint device that records and replays QPainter commands.

Load an image from a file

Qt Load image from a file with QLabel

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.