Conversion between QT qstring and char *

Source: Internet
Author: User

1. Convert qstring to Char
*

First convert qstring to qbytearray, then convert qbytearray to Char
*.

Note: The following conversion form cannot be char * Mm = Str. tolatin1 (). Data ();. In this case, the qbytearray result obtained by Str. tolatin1 () cannot be saved, and the value of MM is null after the final conversion.

 

2. char *
Convert qstring

You can use the constructor of qstring to convert: qstring (const
Qlatin1string & Str );

Constructor of qlatin1string: qlatin1string (const
Char * Str );

The following statement converts char * mm to qstring.
STR:

STR = qstring (qlatin1string (MM ));

 

 

 

Example:

 

// # Include "myclass. H "<br/> # include <qtgui/qapplication> <br/> # include <qtdebug> <br/> # include <qstring> </P> <p> int main (INT argc, char * argv []) <br/>{< br/> qapplication A (argc, argv); </P> <p> qstring STR = "hello "; // convert qstring to char * <br/> qbytearray BA = Str. tolatin1 (); <br/> char * Mm = BA. data (); <br/> qdebug () <mm <Endl; // when debugging, output in the console </P> <p> qstring nn = qstring (qlatin1string (MM); // char * To qstring <br/> qdebug () <NN <Endl; // output in the console during debugging </P> <p> // myclass W; <br/> // W. show (); <br/> return a.exe C (); <br/>}</P> <p>

 

Debug and output the following content on the console:

Hello

Hello

 

 

 

 

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.