QString

From QtCentreWiki

Jump to:navigation, search

A part of QtCore module in Qt4. Represents an Unicode string.

Official web resources

Third party web resources

Please add here!

Common problems

In Qt4 those who don't read the docs carefully often run into problems when they try to convert a QString to char * using QString::toLatin1() or similar method. The proper way to do it is:

QString str( "some string" );
char * cStr = qstrdup( str.toLatin1() );
// use cStr
delete [] cStr;

or:

QString str( "some string" );
QByteArray ba( str.toLatin1() );
const char *cStr = ba.data(); // cStr is a valid pointer as long as ba exists
// use cStr

Constructions like this one:

QString str( "some string" );
char *cStr = str.toLatin1().data(); // ERROR!

won't work, since toLatin1() and other toXxx() methods return a temporary QByteArray, which will be destroyed immediately.

Example code

Please add source code example here!