This example shows how to use low level drawing methods to create an ellipse, rounded rectangle, or a path shape and then set the pen to draw the border and the brush for the fill characteristics.
// ellipse
void DrawArea::paintEvent(QPaintEvent *)
{
QRect rect(30, 60, 240, 180);
QPainter painter(this);
painter.setPen(pen);
painter.setBrush(brush);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.save();
painter.translate(175, 10);
painter.drawEllipse(rect);
painter.restore();
painter.setRenderHint(QPainter::Antialiasing, false);
painter.setPen(palette().dark().color());
painter.setBrush(Qt::NoBrush);
painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
}
// rounded rectangle
void DrawArea::paintEvent(QPaintEvent *)
{
QRect rect(30, 60, 240, 180);
QPainter painter(this);
painter.setPen(pen);
painter.setBrush(brush);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.save();
painter.translate(175, 10);
painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize);
painter.restore();
painter.setRenderHint(QPainter::Antialiasing, false);
painter.setPen(palette().dark().color());
painter.setBrush(Qt::NoBrush);
painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
}
// path
void DrawArea::paintEvent(QPaintEvent *)
{
QPainterPath path;
path.moveTo(60, 240);
path.lineTo(60, 90);
path.cubicTo(240, 0, 150, 150, 240, 240);
QPainter painter(this);
painter.setPen(pen);
painter.setBrush(brush);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.save();
painter.translate(175, 10);
painter.drawPath(path);
painter.restore();
painter.setRenderHint(QPainter::Antialiasing, false);
painter.setPen(palette().dark().color());
painter.setBrush(Qt::NoBrush);
painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
}
Example 32 contained code to draw a polygon in a DrawArea. The source code in this example shows three variations of the DrawArea::paintEvent() method which will render an ellipse, rounded rectangle, or a path shape.
Running the Example
To build and run this example use the source code for “example_32” and replace the DrawArea::paintEvent() with one of the implementations from this example.