QT Basic Data type conversion (int,float,double,datetime,string) __QT

Source: Internet
Author: User

Forwarding: http://blog.csdn.net/tgzjz/article/details/45074237


The method of converting basic data types such as int, float, and double to qstring is roughly as follows:

Take int as an example: [CPP] view plain copy int tmp = 125;             QString str_a = Qstring::number (tmp, 10);   Str_a = = "125" decimal QString Str_b = Qstring::number (tmp,). ToUpper (); Str_b = = "7D" hex
Take float as an example, or:

[CPP] view plain copy float tmpnum = 3.1415;    QString str = QString ("%1"). Arg (Tmpnum); str = "3.1415"
In the work, the above conversion is not used a lot, most of the conversion from the Qstring type to the basic data types used by more, because the QT API has a method, so it is simple to use, the method is roughly as follows:

To change to float, double for example: [CPP] view plain copy QString str = "3.1415";               float Tofloatvalue = Str.tofloat ();     Tofloatvalue = = 3.1415 Double todoublevalue = str.todouble (); Todoublevalue = 3.1415
The above method is more suitable for the representation of the scientific notation of notation:

[CPP] view plain copy Double value = QString ("1234.56e-03"). ToDouble (); Value = 1.23456

It is important to note that the conversion of frequently qstring content to the corresponding basic data type is based on the correctness of the content of the qstring,

In order to improve the robustness of the program, we typically add a bool variable to the conversion to determine the success of the conversion, assuming that the content of the qstring is not recognized as a basic data type, and is used as follows:

[CPP] view plain copy QString str = "HELLOQT";     BOOL OK;  Float value = str.tofloat (&ok); If the conversion fails, value = 0.0, and OK = = false;
Take int for example:

[CPP] view plain copy Qstring str = "FF";     BOOL OK;   int dec = str.toint (&ok, 10); dec==255;   ok==true int hex = Str.toint (&ok, 16);  hex==255; Ok==true;

Conversion to qstring can be done after the conversion of qstring and Qdatetime, the conversion method is as follows:

"" Qdatetime converted to QString

function Prototypes:     QString qdatetime::tostring (Qt::D ateformat format = qt::textdate) const [CPP] view plain copy QString str;     Qdatetime time;     Time = Qdatetime::currentdatetime ();   str = time.tostring ("Yyyy-mm-dd hh:mm:ss"); Strbuffer = 2010-07-02 17:35:00

"" QString converted to Qdatetime

function Prototypes: Qdatetime qdatetime::fromstring (const QString & string, const QString & format) [Static] [CPP] view Plain

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.