What just happened?

The modifications to onPaint that we implemented are creating another path and using that path to fill an area using a gradient. The path is very similar to the original one, but it contains two additional points that are the first and last points drawn projected onto the horizontal axis. This ensures that the gradient fills the area properly. Note that the canvas uses imperative code for drawing; therefore, the order of drawing the fill and the stroke matters—the fill has to be drawn first so that it doesn't obscure the stroke.

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.