Using arguments in strings

A common task is to have a string that needs to be dynamic in such a way that its content depends on the value of some external variable—for instance, you would like to inform the user about the number of files being copied, showing "copying file 1 of 2" or "copying file 2 of 5" depending on the value of counters that denote the current file and the total number of files. It might be tempting to do this by assembling all the pieces together using one of the available approaches:

QString str = "Copying file " + QString::number(current)
            + " of " + QString::number(total);

There are a number of drawbacks to such an approach; the biggest one is the problem of translating the string into other languages, wherein different ...

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.