Convert Std::String to Qstring

Convert std::string to QString

There's a QString function called fromUtf8 that takes a const char*:

QString str = QString::fromUtf8(content.c_str());

How to change string into QString?

If by string you mean std::string you can do it with this method:

QString QString::fromStdString(const std::string & str)

std::string str = "Hello world";
QString qstr = QString::fromStdString(str);

If by string you mean Ascii encoded const char * then you can use this method:

QString QString::fromAscii(const char * str, int size = -1)

const char* str = "Hello world";
QString qstr = QString::fromAscii(str);

If you have const char * encoded with system encoding that can be read with QTextCodec::codecForLocale() then you should use this method:

QString QString::fromLocal8Bit(const char * str, int size = -1)

const char* str = "zażółć gęślą jaźń";      // latin2 source file and system encoding
QString qstr = QString::fromLocal8Bit(str);

If you have const char * that's UTF8 encoded then you'll need to use this method:

QString QString::fromUtf8(const char * str, int size = -1)

const char* str = read_raw("hello.txt"); // assuming hello.txt is UTF8 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const char*
QString qstr = QString::fromUtf8(str);

There's also method for const ushort * containing UTF16 encoded string:

QString QString::fromUtf16(const ushort * unicode, int size = -1)

const ushort* str = read_raw("hello.txt"); // assuming hello.txt is UTF16 encoded, and read_raw() reads bytes from file into memory and returns pointer to the first byte as const ushort*
QString qstr = QString::fromUtf16(str);

How to convert std::string to QString in Qt?

Function QString::fromStdString returns a copy of the string. But in your code result of this function is just ignored. Try this:

const QString str = QString::fromStdString(latitude);

After it you will have QString str which content is the same as content of std::string latitude.

Converting std::string to QString

From qt documentation

QString QString::fromStdString ( const std::string & str ) [static]

Returns a copy of the str string. The given string is converted to
Unicode using the fromAscii() function. This constructor is only
available if Qt is configured with STL compatibility enabled.

You should not use QString::fromUtf8() as std::string are not UTF8 encoded (unless you put that encoded string there somehow)

how to convert std::string to QString

Is your QT compiled with STL compatible enabled option?

Maybe you can use fromUtf8 or one of other static functions of QString.

How to convert QString to std::string?

One of the things you should remember when converting QString to std::string is the fact that QString is UTF-16 encoded while std::string... May have any encodings.

So the best would be either:

QString qs;

// Either this if you use UTF-8 anywhere
std::string utf8_text = qs.toUtf8().constData();

// or this if you're on Windows :-)
std::string current_locale_text = qs.toLocal8Bit().constData();

The suggested (accepted) method may work if you specify codec.

See: http://doc.qt.io/qt-5/qstring.html#toLatin1

Wrapper for converting std::string to QString?

For those who use Qt with QT_NO_CAST_FROM_ASCII enabled char*s and std::strings cannot be assigned to a QString or cast to a QString.

This will not compile with QT_NO_CAST_FROM_ASCII enabled:

QString foo( "Hello World" );

You could do this but Qt frowns upon it because it creates, "temporary QString objects and make a deep copy of the character data":

QString foo( QString::fromLatin1( "Hello World" ) );

Instead you have to use:

QString foo( QLatin1String( "Hello World" ) );

It's a little more frustrating if you need to get a QString from a std::string because you have to now call:

QString foo( QLatin1String( helloWorld.c_str() ) );

There is a direct conversion but not with Qt's recommended wrappers:

QString foo( QString::fromStdString( helloWorld ) );

That is to say I have not been able to find a wrapper for std::string and JKSH asserts that there isn't one. So I believe that as of Qt 5.2 the preferred solution is to use the QLatin1String wrapper.

(As an aside for those interested in multilingual software, Qt's QLatin1String and QString::fromLaint1 both expect the char* to be in proper unicode; QString::fromStdString actually uses QString::fromUtf8 under the hood to convert to proper unicode.)

Converting std::string to QString in constructor

Say hello to the most vexing parse.

StringHandler handler(QString(str)); declares a function named handler that takes a QString and returns a StringHandler. Yes. Thanks to the C++ parsing rules.

Now the error message request for member 'str' in 'handler', which is of non-class type 'StringHandler(QString)' makes sense: handler is being treated like a function of type StringHandler(QString) and you're trying to access a member named str in it, but of course functions have no members so the compilation fails.

You can fix this by using the uniform initialization syntax:

StringHandler handler{QString(str)};

The above can't be parsed as a function declaration, and so the compilation should fail for the expected reason: no matching constructor QString(std::string).

For more info, see C++'s most vexing parse again.



Related Topics



Leave a reply



Submit