How it works...

Each of the arrays contain three QPoint data instances, which form the shape of an elongated triangle. The arrays are then passed to the painter and rendered as a convex polygon using the drawConvexPolygon() function. Before drawing each of the clock hands, we use painter.save() to save the state of the QPainter object and then proceed with drawing the hand using coordinate transformation.

Once we're done with the drawing, we restore the painter to its previous state by calling painter.restore(). This function will undo all the transformations before painter. restore() so that the next clock hand will not inherit the transformations of the previous one. Without using painter.save() and painter.restore(), we will have to manually change back the position, rotation, and scale before drawing the next hand.

A good example of not using painter.save() and painter.restore() is when drawing the dials. Since each dial's rotation is an increment of 6 degrees from the previous one, we don't need to save the painter's state at all. We just have to call painter.rotate(6.0) in a loop and each dial will inherit the previous dial's rotation. We also use a modulus operator (%) to check whether the unit represented by the dial can be divided by 5. If it can, then we draw it slightly longer.

Without using a timer to constantly call the update() slot, the clock will not function properly. This is because paintEvent() will not be called by Qt when there is no change to the state of the parent widget, which in this case is the main window. Therefore, we need to manually tell Qt that we need to refresh the graphics by calling update() every second. We used the painter.setRenderHint(QPainter::Antialiasing) function to enable anti-aliasing when rendering the clock. Without anti-aliasing, the graphics will look very jagged and pixelated:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset