-
Notifications
You must be signed in to change notification settings - Fork 28
Knowledgebase Programming: Qt Strings
Lars Toenning edited this page Dec 11, 2023
·
2 revisions
The best way to know which code construct to use in a given situation is to understand the API of the facilities you use, and what they provide.
-
QString
-
QString
is a class that owns a shared, dynamically-allocated UTF-16 string. - The sharing is detached (so the string data is copied) when any non-const operation is performed (copy-on-write). When constructing a
QString
from a string literal, the data is always copied.
-
-
QStringLiteral
-
QStringLiteral
is a macro that "returns" aQString
- The internal shared data of the
QString
is prepared at compile-time, not dynamically allocated, and not copied. If you have a string literal, and you need aQString
, thenQStringLiteral
is what you want. But you don't always need aQString
. In situations where you don't need aQString
, it is a waste of time to useQStringLiteral
.
-
-
QLatin1String
-
QLatin1String
is a class that holds aconst char*
pointing to a string literal. - Its use cases are limited, as converting it to a
QString
involves converting the Latin-1 data into UTF-16. Certain Qt functions are overloaded forQLatin1String
because they can directly use the Latin-1 data without converting it to UTF-16. For example, JSON keys can be passed asQLatin1String
. In such cases, using aQString
would perform an unnecessary allocation and convertion from Latin-1 to UTF-16 and back again.
-
-
QStringView
-
QStringView
is a class that holds aconst char16_t*
pointing to some UTF-16 string. - It is often useful as a function parameter type. If you write a function that takes a string parameter, which it uses and then discards, it can be beneficial to declare the parameter as
QStringView
. The caller can then choose to pass a string to your function using whatever type is most convenient to them. If they had achar16_t*
then they would not be forced to construct an unnecessaryQString
just to be able to pass it to your function. - If your function really does need a
QString
, don't useQStringView
as it would inhibit the implicit sharing feature ofQString
. BecauseQStringView
is such a simple class, it is more efficient to pass it by value, not reference.
-
-
QStringRef
-
QStringRef
is a class that references a substring of an existingQString
without copying it. - This can be useful when working with substrings, where you would otherwise be allocating unnecessary temporary
QString
instances to copy substrings into them. For example,s.leftRef(3).trimmed()
instead ofs.left(3).trimmed()
. - Of particular note is
QString::splitRef
, which is likesplit
except that it returns aQVector<QStringRef>
instead of aQStringList
. UsingQStringList
would be very inefficient if you just want to loop over the parts, as it would reallocate and copy every single one for no reason. - Just make sure that the
QStringRef
gets destroyed //before// its referencedQString
, otherwise this would be a dangling reference.
-
-
QChar
-
QChar
is a class that contains a single UTF-16 character.
-
-
QLatin1Char
-
QLatin1Char
is a class that contains a single Latin-1 character.
-
-
QString::arg
-
arg
is a member function ofQString
,- so obviously if you want to call
arg
then you need aQString
to call it on. So if you want to callarg
on a string literal, then you almost always wantQStringLiteral
. The same goes for other member functions ofQString
. But note thatQStringView
andQStringRef
provide many of the same methods.
- so obviously if you want to call
-
-
operator %
- The % operator provides more efficient string concatenation when
<QStringBuilder>
is included. - Instead of using lots of temporary
QString
objects to store each subexpression (like+
does) it collects the parts together and concatenates them all in one go. - You should look in the
qstringbuilder.h
header to see which types can be concatenated. The class templateQConcatenable
is specialised on each concatenable type:QString
,QStringRef
,QStringView
char16_t*
, etc. So for string literals, these can be concatenated directly as UTF-16 literals, without anyQString
-based intermediary - One rare exception to the above rule: if every type in a given concatenation is a fundamental type like
char*
orchar16_t*
, it will fail to compile. This is because operators can obviously not be overloaded on built-in types. At least one of the parts of the concatenation must be a class type. The best workaround isQStringView
as it is the most lightweight.
- The % operator provides more efficient string concatenation when
It can take time and experience to cement the understanding in the brain so that it becomes intuitive (to become "fluent"). Therefore when working with string literals you can use the following cheat sheet that should cover most situations:
Empty string | Single character | Multiple characters | |
---|---|---|---|
Store in a QString
|
QString() |
QStringLiteral |
QStringLiteral |
Concatenate with %
|
u' ' |
u" " |
|
As QLatin1String
|
QLatin1String() |
QLatin1String |
QLatin1String |
As QStringView parameter |
u"" |
u" " |
u" " |
- Home
- API documentation (Doxygen generated)
- Future of swift
- Style and Coding Standard
- Build swift
- Run swift as a developer
- Knowledgebase
- External resources
- Open Research Questions
- Aviation
- Programming
- Simulation
- Architecture