A Guide to Creating Custom Graphics in Qt GUIs with Painting Classes


Core Class: QPainter

  • QPainter also supports transformations for scaling, rotating, or translating your drawings (QTransform).
  • You can define styles for lines and fills using QPen and QBrush classes respectively.
  • It offers functions to draw various graphical elements like:
    • Lines, rectangles, polygons, ellipses
    • Complex shapes using QPainterPath
    • Text with font control (QFont)
    • Images (QPixmap)
  • This is the workhorse for drawing on widgets.
  • QFont
    Controls the appearance of text drawn by QPainter, specifying font family, size, and styles.
  • QBrush
    Defines how the interior of shapes are filled, with options for solid colors, patterns, gradients, etc.
  • QPen
    Controls the style of lines drawn by QPainter, including width, color, and end-cap styles.
  • QPainterPath
    Defines a path for complex shapes that can be reused for efficient drawing.
  • QPixmap
    Represents an image object for drawing raster graphics.


#include <QApplication>
#include <QWidget>
#include <QPainter>

class ScribbleArea : public QWidget {
  Q_OBJECT

public:
  ScribbleArea(QWidget *parent = nullptr) : QWidget(parent) {}

protected:
  void paintEvent(QPaintEvent *event) override {
    QPainter painter(this);

    // Draw a blue rectangle
    painter.setPen(QPen(Qt::blue, 2));
    painter.drawRect(50, 50, 100, 150);

    // Draw a red circle
    painter.setPen(QPen(Qt::red, 5));
    painter.setBrush(Qt::red);
    painter.drawEllipse(200, 50, 80, 80);

    // Draw some text
    painter.setFont(QFont("Arial", 16));
    painter.drawText(QRect(100, 250, 200, 30), Qt::AlignCenter, "Qt Painting Example");
  }
};

int main(int argc, char *argv[]) {
  QApplication app(argc, argv);

  ScribbleArea scribble;
  scribble.resize(400, 300);
  scribble.show();

  return app.exec();
}
  1. We define a ScribbleArea class that inherits from QWidget.
  2. In the paintEvent function, we create a QPainter object to draw on the widget.
  3. We use setPen and setBrush methods of QPainter to define the style of lines and fill for our shapes.
  4. We draw a rectangle, a circle, and some text using various functionalities of QPainter.
  5. In the main function, we create an instance of ScribbleArea, set its size, and show it on the screen.


Qt Quick with QQuickPaintedItem

  • While it offers a different approach compared to C++ and QPainter, it allows for a more declarative and potentially faster development process, especially for simpler UI elements.
  • QQuickPaintedItem is a QML type specifically designed for custom drawing within Qt Quick applications.
  • Qt Quick is a declarative UI framework within Qt that allows you to define your UI using a JavaScript-like language called QML.

Third-party Libraries

  • Examples include:
    • OpenGL: A low-level graphics API for high-performance and complex 2D/3D rendering.
    • SDL (Simple DirectMedia Layer): Another popular library for multimedia applications, including graphics.
    • Specialized libraries for specific needs like vector graphics (e.g., Cairo) or image processing (e.g., OpenCV).
  • Several third-party libraries can be integrated with Qt for advanced graphics needs.

Hardware Acceleration

  • Qt provides mechanisms to leverage these capabilities for improved performance, especially when dealing with complex graphics or animations.
  • Modern GPUs offer powerful hardware acceleration capabilities.

Choosing the Right Option

The best alternative depends on your specific needs:

  • High-performance Graphics
    For complex graphics or animations, consider third-party libraries like OpenGL or hardware acceleration.
  • Declarative UI and Faster Development
    If you're using Qt Quick and need custom drawing elements, QQuickPaintedItem is a good alternative.
  • Simple Drawing
    Qt's Painting Classes (particularly QPainter) are often the easiest and most efficient choice for basic drawing needs within your Qt GUI application.