How to Change String into Qstring

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);

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 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)

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.

Converting QString from string in a file

You need to know what encoding (charset) is used in your file. Then you will either use fromUtf8, or something else - using the QTextCodec.

Example from Qt docs:

QTextCodec *codec = QTextCodec::codecForName("Shift-JIS");
QTextDecoder *decoder = codec->makeDecoder();

QString string;
while (new_data_available()) {
QByteArray chunk = get_new_data();
string += decoder->toUnicode(chunk);
}
delete decoder;

How to convert int to QString?

Use QString::number():

int i = 42;
QString s = QString::number(i);

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



Related Topics



Leave a reply



Submit