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

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

   //! dtor
   ~PainterWidget()
   {}

protected:

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

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

};

Retrieved from http://schorsch.efi.fh-nuernberg.de/roettger/index.php/MedicalVisualization/QtPainterExample

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