Show Menu
Cheatography

C++ (QString) Cheat Sheet (DRAFT) by

A cheatsheet on the Qt framework

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Qstring

// Qt's Unicode string class, used wherever you would use std::string in a Qt codebase. It handles 
// international characters and encodings natively, and provides methods for common string operations // like splitting, trimming, and replacing. It interoperates with std::string via fromStdString() and  
// toStdString().

QString qString = QString::fromStdString(stdString);
QString combined = QStringLiteral("literalPrefix") + qString;

QStrin­g::­fro­mSt­dString

// A static factory method on QString that converts a std::string into a QString, bridging the standard 
// C++ and Qt string ecosystems. It internally constructs a new QString by copying the character data 
// across from the std::string. It is called on the class itself rather than an instance, signalling that it is a 
// QString concern.

QString qString = QString::fromStdString(stdString);

QStrin­gLi­teral

QString literal = QStringLiteral("hardcodedValue");
A Qt macro that creates a QString from a hardcoded string literal at compile time rather than runtime, avoiding any memory alloca­tion. It is used wherever you have a fixed string value baked directly into the code rather than a variable. For dynamic values coming from variables, QStrin­g::­fro­mSt­dSt­ring() is used instead.

QStrin­g::­fro­mLo­cal8Bit

QString errorOutput = QString::fromLocal8Bit(rawBytes);
A static factory method on QString that converts raw byte data into a QString using the local system's text encoding. It is used specif­ically for reading process error output since readAl­lSt­and­ard­Error() returns raw bytes rather than a proper string. It is preferred over fromSt­dSt­ring() sometimes because error messages from system processes are encoded in the local system encoding rather than UTF-8.

QStrin­g::­tri­mmed()

QString cleaned = qString.trimmed();
A method on QString that returns a copy of the string with all leading and trailing whitespace removed, including spaces, newlines, and tabs. It is commonly used when reading output from external processes since they often append a newline to the end of their output. It does not modify the original string but returns a new cleaned copy.