The transformation of qstring and char* in QT

Source: Internet
Author: User

The transformation of these basic data types can sometimes make us a headache. Do not write, dare not write, write afraid of the wrong. Although now running QT down, this did not meet the first time a data type conversion is required.

First of all, the QT qstring how to turn char* it.

int main (int argc, char **argv)
{
Qapplication app (argc, argv);
QString str1 = "Test";
Qbytearray ba = Str1.tolatin1 ();
const char *C_STR2 = Ba.data ();
printf ("str2:%s", C_STR2);
return App.exec ();
}
Note here that the middle BA variable is essential and cannot be directly used with str1.tolatin1 (). Data ().

And then it's char* turn qstring:

Method One: QString string = QString (qlatin1string (C_STR2));

Method Two: If the Chinese system, directly with (const char*) str.local8bit ()
For example
printf ("%s", (const char*) str.local8bit ());
STR is a qstring

Recorded some of their own learning process of knowledge, hope to help you, also hope to reprint can retain the original address. Thanks.

Beyond C + + original articles, reproduced please indicate the source and keep the original link

This article link: http://www.beyondc.cn/qt-in-the-qstring-and-char-conversion.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.