Nominate our 2022 Qt Champions!

So after QString::asprintf was removed, are we supposed to implement printf patterns by ourselves? Why?

  • So QString::asprintf known in Qt4 was removed in Qt5. Now we're advised to use the arg method of QString. It has many features allowing application extensibility and whatewer, but doesn't support printf format.

    I have a program where user can specify table-cell formatting using printf format in formatter variable. Since you removed asprintf without replacement, I ended up with about two options:

    1. Do insane things like this:

         QByteArray FSA = formater.toLatin1();
         const char*  fs =;
         // Convert format to std::string - See locale pseudo-singleton below
         boost::format f( fs, STDLocaleSingleton::Instance() );
         // And convert back to QString
         return QString::fromLatin1( boost::str( f % valueToBeFormated->toDouble() ).c_str() );
         // Probably saves some memory, I think
         class STDLocaleSingleton {
             static const std::locale &Instance(){
                 static std::locale loc("");
                 return loc;
    2. Write the printf code interpreter myself?

    How do you guys format strings using printf codes now, when Qt decided they know better?

    PS.: Nice quotation from docs, underlining how awesome the new functions are:

    If fillChar is '0' (the number 0, ASCII 48), this function will use the locale's zero to pad. For negative numbers, the zero padding will probably appear before the minus sign.

  • Moderators

    Hi, welcome to devnet.

    Since you removed asprintf (...)

    We didn't. This forum is mostly used by users, not creators of Qt ;)

    I have a program where user can specify table-cell formatting using printf format

    That's your choice of a format. Qt doesn't have to support everything you need in every possible situation. My guess is Qt moved away from sprintf-like functions because they are already provided by std::, with which Qt's implementation wasn't fully compatible anyway, they are also i18n and unicode unfriendly.

    If you need sprintf formatting why not just use sprintf?

  • Lifetime Qt Champion

    If you really do not want to use .arg,
    There is also the option to take qstring.cpp
    from version that has it and make it work again.

    QString &QString::vsprintf(const char* cformat, va_list ap)
    seems not so long.

    QString &QString::sprintf(const char *cformat, ...)
        va_list ap;
        va_start(ap, cformat);
        QString &s = vsprintf(cformat, ap);
        return s;

  • Moderators

    @MXXIV said:

    So QString::asprintf known in Qt4 was removed in Qt5.

    It's QString::sprintf() that was in Qt 4 but removed in Qt 5.

    Qt 5.5 introduced QString::asprintf() (and QString::vasprintf()), in fact: (Hmm... there seems to be a documentation bug, and these functions aren't showing up. However, they are there for you to use)