-
Notifications
You must be signed in to change notification settings - Fork 33
Knowledgebase Programming: Qt Strings
Lars Toenning edited this page Oct 9, 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-
QStringis 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
QStringfrom a string literal, the data is always copied.
-
-
QStringLiteral*QStringLiteralis a macro that "returns" aQString* The internal shared data of theQStringis prepared at compile-time, not dynamically allocated, and not copied. If you have a string literal, and you need aQString, thenQStringLiteralis 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-
QLatin1Stringis a class that holds aconst char*pointing to a string literal. - Its use cases are limited, as converting it to a
QStringinvolves converting the Latin-1 data into UTF-16. Certain Qt functions are overloaded forQLatin1Stringbecause 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 aQStringwould perform an unnecessary allocation and convertion from Latin-1 to UTF-16 and back again.
-
-
QStringView-
QStringViewis 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 unnecessaryQStringjust to be able to pass it to your function. - If your function really does need a
QString, don't useQStringViewas it would inhibit the implicit sharing feature ofQString. BecauseQStringViewis such a simple class, it is more efficient to pass it by value, not reference.
-
-
QStringRef-
QStringRefis a class that references a substring of an existingQStringwithout copying it. - This can be useful when working with substrings, where you would otherwise be allocating unnecessary temporary
QStringinstances to copy substrings into them. For example,s.leftRef(3).trimmed()instead ofs.left(3).trimmed(). - Of particular note is
QString::splitRef, which is likesplitexcept that it returns aQVector<QStringRef>instead of aQStringList. UsingQStringListwould 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
QStringRefgets destroyed //before// its referencedQString, otherwise this would be a dangling reference.
-
-
QChar-
QCharis a class that contains a single UTF-16 character.
-
-
QLatin1Char-
QLatin1Charis a class that contains a single Latin-1 character.
-
===== String consumers =====
-
QString::arg-
argis a member function ofQString,- so obviously if you want to call
argthen you need aQStringto call it on. So if you want to callargon a string literal, then you almost always wantQStringLiteral. The same goes for other member functions ofQString. But note thatQStringViewandQStringRefprovide 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
QStringobjects to store each subexpression (like+does) it collects the parts together and concatenates them all in one go. - You should look in the
qstringbuilder.hheader to see which types can be concatenated. The class templateQConcatenableis specialised on each concatenable type:QString,QStringRef,QStringViewchar16_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 isQStringViewas 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
- Release Checklist
- Build swift
- Run swift as a developer
- Knowledgebase
- External resources
- Open Research Questions
- Aviation
- Programming
- Simulation
- Architecture