Time for action – A simple quiz game

To introduce you to the main usage of QRegularExpression, let's imagine this game: a photo, showing an object, is shown to multiple players, and each of them has to estimate the object's weight. The player whose estimate is closest to the actual weight wins. The estimates will be submitted via QLineEdit. Since you can write anything in a line edit, we have to ensure that the content is valid.

So what does valid mean? In this example, we define that a value between 1g and 999kg is valid. Knowing this specification, we can construct a regular expression that will verify the format. The first part of the text is a number, which can be between 1 and 999. Thus, the corresponding pattern looks like [1-9]\d{0,2} ...

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.