Time for action – optimizing oscillogram drawing
As an exercise, we will modify our oscillogram widget so that it only re-renders the part of its data that is required. 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 *pe) { QRect exposedRect = pe->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) { // ... QRect selectionRect = rect; selectionRect.setLeft(m_pressX); selectionRect.setRight(m_releaseX); ...