VolumeRendering

QPainter Example

Qt Signal and Slots | | Qt Main Window

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()
   {}

   //! return preferred minimum window size
   QSize minimumSizeHint() const
   {
      return(QSize(100, 100));
   }

   //! return preferred window size
   QSize sizeHint() const
   {
      return(QSize(512, 512));
   }

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");
   }

};


Qt Signal and Slots | | Qt Main Window

Options: