From Ohm

QPainter Example

QPainter is a Qt class that serves as a renderer for 2D graphic primitives like lines, polygons and text. It renders either into pixmaps (QPixmap), images (QImage) or into a widget, which inherits from QPixmap, as well.

In the following we create a new widget type by deriving from QWidget and draw a green text as background of the new widget. For that purpose we need to override the paintEvent() method, which is called whenever the widget’s graphical representation needs to be repainted:

#include <QtGui/QWidget>

class PainterWidget: public QWidget

   //! default ctor
   PainterWidget(QWidget *parent = 0)
      : QWidget(parent)

   //! dtor


   // reimplemented paint event
   void paintEvent(QPaintEvent *)
      QPainter painter(this);

      painter.setFont(QFont("Arial", 100));
      painter.drawText(rect(), Qt::AlignCenter, "Qt");


Retrieved from

Page last modified on November 13, 2015, at 09:33 PM