[Interest] QByteArray vs QString, arg, why is there no arg()?

Jason H jhihn at gmx.com
Wed Sep 18 15:57:11 CEST 2019

> Sent: Wednesday, September 18, 2019 at 2:50 PM
> From: "Giuseppe D'Angelo via Interest" <interest at qt-project.org>
> To: interest at qt-project.org
> Subject: Re: [Interest] QByteArray vs QString, arg, why is there no arg()?
> Il 18/09/19 13:16, Jason H ha scritto:
> > What's the best way to zero-pad a QByteArray?
> > What I want is QByteArray("%1").arg(6, 10, 10, '0')
> Mostly it has to do with the fact that QByteArray is sitting between two
> worlds; on one side it's just a container of bytes, on the other side it
> has _some_ manipulation functions for ASCII-like strings. "Some"
> because, as you've noticed, stuff like the arg() convenience is missing.
> If you really need a QByteArray you can work around this by e.g. using a
> printf-like function, what you're looking for is the "%010d" formatting.
> Pseudocode:
> int n = 123;
> const char *format = "The number is %010d";
> auto size = qsnprintf(nullptr, 0, format, n);
> QByteArray result(size, Qt::uninitialized);
> qsnprintf(result.data(), result.size(), format, n);

That helps, but wasn't the answer I wanted to hear. I have to call qsnprintf() twice. Granted, for valid reasons.and it might be faster than my gluing things together with +.

I do prefer python's approach where the character size change does not come with an API change. Maybe discussion for Qt6?. I would think they C++ way would be to have 1 API and a template class? (Though I think some people cringe at that idea)

More information about the Interest mailing list