QPainter Example
← Qt Widgets | ● | Qt with CMake →
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:
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");
}
};
← Qt Widgets | ● | Qt with CMake →