[Interest] Efficient QString construction

Thiago Macieira thiago.macieira at intel.com
Tue Dec 15 13:32:33 CET 2015


On Tuesday 15 December 2015 10:02:09 Julius Bullinger wrote:
> Hello,
> 
> What is the currently recommended way to concatenate several QStrings and/or
> string literals? Take this code as an example:

Using the fast operator +.

DEFINES += QT_USE_FAST_OPERATOR_PLUS

> QString basename = QFileInfo(imgPath).baseName();
> 
> QString thumbPath = getProjectDir() + "/thumb_" + basename + ".jpg";
> 
> img.save(thumbPath);
> 
> 
> I found several blog posts and wiki articles recommending one or multiple
> of:
> 
> 
> 1.       QStringLiteral

QStringLiteral is to be used when you want to actually use the literal as the 
whole string. That's not the case here.

> 2.       QLatin1String

You should use QLatin1String whenever there's an overload that takes a 
QLatin1String. You'd need here it if you compiled with QT_NO_CAST_FROM_ASCII.

> 3.       QStringBuilder

This is the fast operator+.

> 4.       Simple string literals

This is the slowest.

> Since this may have changed over time, what do current best practices say?

The same as they've always said.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Interest mailing list