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 informatter
variable. Since you removedasprintf
without replacement, I ended up with about two options:-
Do insane things like this:
QByteArray FSA = formater.toLatin1(); const char* fs = FSA.data(); // 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 { STDLocaleSingleton(){}; public: static const std::locale &Instance(){ static std::locale loc(""); return loc; } };
-
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.
-
-
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?
-
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.
andQString &QString::sprintf(const char *cformat, ...) { va_list ap; va_start(ap, cformat); QString &s = vsprintf(cformat, ap); va_end(ap); return s; }
-
@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()
(andQString::vasprintf()
), in fact: http://code.woboq.org/qt5/qtbase/src/corelib/tools/qstring.cpp.html#_ZN7QString8asprintfEPKcz (Hmm... there seems to be a documentation bug, and these functions aren't showing up. However, they are there for you to use)