Conversion of qstring and number types in QT

Source: Internet
Author: User
Tags current time time 0

Convert qstring to double type

Method 1. QString str= "123.45";

Double val=str.todouble (); val=123.45

Method 2: Very suitable for scientific counting method form Conversion


Double D;

D=qstring ("1234.56e-02"). ToDouble (&ok); ok=true;d;12.3456.

Convert qstring to float

1.QString str= "123.45";

float d=str.tofloat (); d=123.45

2.QString str= "R2D2";


Float d=str.tofloat (&ok); The conversion is returned by the time 0.0,ok=false;

Convert the qstring shape into plastic

1. Converting to decimal shaping

Note: The cardinality defaults to 10. When the base is 10 o'clock, and the cardinality must be 2 to 36

Room If the cardinality is 0, if the string starts with 0x, it will be converted to 16, and if it starts with a 0, it is converted to octal, otherwise it will be converted to decimal.

Qstring str= "FF";


int Dec=str.toint (&ok,10); dec=255; Ok=rue

int hex =str.toint (&ok,16); Hex=255;ok=true;

3. Normal shaping into qstring shape

Long A = 63;

QString Str=qstring::number (a,16); Str= "3f";

QString Str=qstring::number (a,16). ToUpper (); Str= "3F"; Qstring conversion char* problem!

Method one: [The net of drinking small wine]
QString qstr ("Hello,word");
const char * p = qstr.tolocal8bit (). data ();
Method Two:
const char *p = qstr.tostdstring (). data ();
The conversion is constant to convert the current time to qstring ...

Public Qdatetime qdate = Qdatetime.currentdatetime ();
datetime = qdate.tostring ("yyyy mm month DD Day Ddddhh:mm:ss");
If not qtime and Qdate classes such as: How to convert a char unsigned char class received via TCP/IP to a qstring class
QString time2string (DWORD dwtime)
Char ctime[50] = {0};
memset (ctime,0,50);
Strftime (ctime,32, "%y-%m-%d%h:%m:%s", LocalTime (&time_t (Dwtime)));
Return QString (CTime);
} [the net of drinking wine pick]

reprinted from [] drink small wine net pick and this article link address:

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: 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.