What just happened?

First, we added a setupRod() helper function that creates a new rod and connects its signal to the new rodClicked() function. Then we simply called setupRod() for each index and accumulated the rod object into the rods array. The rodClicked() function will contain the rest of our game logic, but for now it only prints the index of the clicked rod to the application output.

Note that the content of the setupRod() function cannot be placed directly into the body of the for loop over i. The clicked() signal is connected to a closure that captures the rod variable. Within the function, each rod will connect to a closure that captures the corresponding Rod object. Within the for loop, all closures would capture the common

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.