Time for action – Optimizing oscillogram drawing

The first step is to modify the paint event handling code to fetch information about the region that needs updating and pass it to the method drawing the chart. The changed parts of the code have been highlighted here:

void Widget::paintEvent(QPaintEvent *event)
{
    QRect exposedRect = event->rect();
    ...
    drawSelection(&painter, r, exposedRect);
    drawChart(&painter, r, exposedRect);
    painter.restore();
} 

The next step is to modify drawSelection() to only draw the part of the selection that intersects with the exposed rectangle. Luckily, QRect offers a method to calculate the intersection for us:

void Widget::drawSelection(QPainter *painter, const QRect &rect, const QRect &exposedRect) { // ... ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.