Part of the visa process
GOV.UK puts the onward-travel rule straight after its explanation of the tourist visa, also called a tourist card. You can get one through the Cuban Embassy, your travel agent or tour operator, or the official eVisa site. An approved eVisa gives you a code to enter on the D'Viajeros advance passenger platform, which produces the QR code you show at the border.
A two-part passport rule
Your passport must be valid for at least 6 months after you arrive and 3 months after your planned departure. The second part depends on the exit date your onward booking shows, so check both.