I am drawing a sine wave (as a test) to check the time it takes to draw a curve using either QPainter::drawPolyline()
or QPainter::drawPath()
. In either case, if I set the pen width to more than 1, then the time it takes to draw the sine wave increases by more than 10 times.
Widget::Widget(QWidget* parent)
: QWidget(parent)
{
setAutoFillBackground(true);
setAttribute(Qt::WA_NoSystemBackground, true);
// creating the sine wave
for(qsizetype ii = 0; ii < 1000000; ++ii)
sineWave << (1.0 + sin(2 * M_PI * 3 * ii / 1000000));
}
void Widget::paintEvent(QPaintEvent* evt)
{
QPainter painter(this);
painter.fillRect(rect(), QColor(Qt::white));
painter.setRenderHint(QPainter::Antialiasing);
QPen pen;
pen.setWidthF(2); // setting pen width
painter.setPen(pen);
QElapsedTimer timer;
timer.start();
qDebug() << "-> Paint Event\tPen Width:" << pen.widthF() << "\tWidget WxH:" << width() << "x" << height();
QList<QPointF> pixPoints;
for(qsizetype ii = 0; ii < sineWave.length(); ++ii) {
double yPos = height() * (sineWave[ii] / 2.0);
double xPos = width() * (ii / 1000000.0);
pixPoints << QPointF(xPos, yPos);
}
qDebug() << "--> 1: Time [ms]:" << (timer.nsecsElapsed() / 1000000.0);
painter.drawPolyline(pixPoints);
qDebug() << "--> 2: Time [ms]:" << (timer.nsecsElapsed() / 1000000.0);
}
The output when I set the pen width = 2
-> Paint Event Pen Width: 2 Widget WxH: 722 x 494
--> 1: Time [ms]: 109.352
--> 2: Time [ms]: 5887.08
The output when I set the pen width = 1
-> Paint Event Pen Width: 1 Widget WxH: 741 x 535
--> 1: Time [ms]: 105.882
--> 2: Time [ms]: 233.165
The time taken for creating the QList<QPointF> pixPoints
in both cases is the similar (marked as 1:
), but the time it takes to draw the curve using QPainter::drawPolyline()
function (marked as 2:
) is extremely high when I set the pen width to 2 compared to when the pen width is set to 1.
Is there any way I could reduce this time gap?
P.S. I have posted the same question on Qt Forum here