2D Painting Example

  • In following example: "2D Painting":https://qt-project.org/doc/qt-5.0/qtopengl/2dpainting.html
    Please someone can explain me paint function in helper class??

    @void Helper::paint(QPainter *painter, QPaintEvent *event, int elapsed)
    painter->fillRect(event->rect(), background);
    painter->translate(100, 100);
    painter->rotate(elapsed * 0.030);
    qreal r = elapsed / 1000.0;
    int n = 30;
    for (int i = 0; i < n; ++i) {
    qreal factor = (i + r) / n;
    qreal radius = 0 + 120.0 * factor;
    qreal circleRadius = 1 + factor * 20;
    painter->drawEllipse(QRectF(radius, -circleRadius,
    circleRadius * 2, circleRadius * 2));
    painter->drawText(QRect(-50, -50, 100, 100), Qt::AlignCenter, QStringLiteral("Qt"));

    Logic behind expressions and values??

  • What specific part don't you understand?

  • [quote author="mickey" date="1373218940"]Please someone can explain me paint function in helper class??[/quote]

    You can learn details about method of "QPainter used at the code above from the official documentation":http://qt-project.org/doc/qt-5.0/qtgui/qpainter.html.

  • [quote author="DerManu" date="1373222871"]What specific part don't you understand?[/quote]

    The values calculated like in line 8 "elapsed *0.03" and in line 14,15...

  • Well that just means that the rotation shall be advanced by 0.03 per "elapsed tick". The number 0.03 has no special meaning other than that the programmer was happy with the speed. 0.04 or 0.02 would also have been good enough.

    Same thing with lines 14 and 15. Just describing the spiral (increasing orbit of the ellipse center) and the increasing ellipse size with two linear functions dependant on the ellipse index (there are 30 ellipses).

  • Thanx I understood almost...

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.