How to convert qstring to char * or vice versa

Source: Internet
Author: User

Source: http://duanyuxue.blog.163.com/blog/static/43731519200892372722425/

 

Originally, QT's qstring is quite useful, but OpenGL shading language uses char *, especially the glshadersource () function. Therefore, the conversion between them should be considered.
Direct conversion is not acceptable because qstring does not provide direct member functions, but you can use qbytearray to transfer it. For example:
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.exe C ();
}

 

There are other methods:

Method 1 -----------------------------------------
# Define g2u (s) (qtextcodec: codecforname ("GBK")-> tounicode (s ))
# Define u2g (s) (qtextcodec: codecforname ("GBK")-> fromunicode (s ))

Qstring STR;
Qcstring CSTR;

STR = g2u ("Chinese Input ");
CSTR = u2g (STR );

Qcstring has such an overload Operator
Operator const char * () const

Yes.
Printf ("% s/n", (const char *) CSTR );
Or copy it out.
Char Buf [1024];
Strcpy (BUF, (const char *) CSTR );

 

Method 2 -----------------------------------------
For a Chinese System

Directly use (const char *) Str. local8bit ()
For example
Printf ("% s", (const char *) Str. local8bit ());

STR is a qstring

Method 3 -----------------------------------------
Char STR [64];
Qtextcodec * textcod = qtextcodec: codecforname ("GBK ");
Qcstring string1 = textcod-> fromunicode (listbox1-> currenttext ());
Strcpy (STR, string1 );

Qstring and STD: String

From char * To qstring, it can be converted from fromlocal8bit ()
STD: string functions with c_str () are converted to char *

Qstring has toascii () and cannot be remembered

You can check it out.

My carelessness caused a big mistake. I checked the QT document again. Originally, QT could generate a qstring directly from STD: wstring and use qstring: fromstdwstring (const STD :: wstring. I tried to use the qstring constructed by char * returned by c_str () of STD: String. instead of saving the original Chinese information, I used STD :: qstring constructed by wstring can use qdebug () to output the original Chinese information.
Conversion between GB encoding and utf8 Encoding
Add this sentence after the main function app:

Quote:

Qtextcodec: setcodecforlocale (qtextcodec: codecforname ("gb18030 "));

 

The conversion method from utf8 encoding to GB encoding is as follows:

Quote:

Qstring utf8_to_gb (qstring strtext)
{
Return qstring: fromutf8 (strtext. tolocal8bit (). Data ());
}

 

From GB to utf8, we often use:

Quote:

Qstring gb_to_utf8 (char * strtext)
{
Return qstring: fromlocal8bit (strtext );
}

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.