QT笔记2(常用数据类型)

1、调试类型

#include<QDebug>

qDebug()<<"调试信息";

2、可变类型(通用类型)

#include <QVariant>

QVariant v;

v = 99 ;

qDebug()<<v.toInt();

v ="kufei"

qDebug()<<v.toString();

3.正则表达式

#include<QRegExp>

 C++变量名规则

QRegExp re("[a-zA-Z]+[a-zA-Z0-9_]*");

string val;

while(1)

{

     cin>>val;

     if(val =="0")

           break;

    if(re.exactMatch(val.c_str()))

       qDebug<<"ok";

   else

         qDebug<<"fail";

}

4、时间和日期

#include<QTime>

#include<QDate>

qDebug()<<QDate::currentDate().toString();

qDebug()<<QTime::currentTime().toString();

获取当前日期

qDate d=currentDate();

qDebug<<d.day();当前几号

qDebug<<d.month();当前几号

qDebug<<d.year();当前年份

qDebug<<d.dayofWeek();当前本周第几天

qDebug<<d.daysInMonth();当月总共有几天

获取当前时间

QTime t = QTime::currentTime();

qDebug()<<QString("时:%1").argue(t.hour());

qDebug()<<QString("分:%1").argue(t.minute());

qDebug()<<QString("秒:%1").argue(t.second());

备注:qPrintable无引号输出

5、链表(模板STL)

#include<QList>

QList<int> list;

list<<1;

list<<2;

list<<3;

list.append(4);

list.append(5);

list.append(6);

//C++迭代器遍历容器

for(QList<int>::iterator it=list.begin();

     it != list.end();it++)

{

      qDebug()<<*it;

}

//枚举for循环遍历容器

for(int a:list)

{

        qDebug()<<a;

}

//java 风格遍历迭代器

QListIterator<int> it(list)

while(it.hasNext())

{

  qDebug()<<it.next();

}

6、字节数组

#include<QByteArray>

#include<QFile>

评析:在存储字符串的场合中,QString是首先,使用QByteArray的场合:

1、存储二进制数据

2、内存空间资源有限,对内存要求苛刻

QByteArray baText;

QByteArray baBin;

baText.append("100");存储文本

int a = 100;

baBin.append((const char *)&a,sizeof(a));存放二进制

QFile filText("a.txt");

filetext.open(QIODevice::writeonly);只写模式打开

fileText.write(baText);

fileText.close();

QFile fileBin("b.bin");

fileBin.open(QIODevice:WriteOnly);只写模式打开

fileBin.Write(baBin);

fileBin.close();

7、字符串

#include<QString>

QString s1("abc");

QString s2("www");

qDebug()<<s1+s2;

QString s3 = QString("%1,%2,%3").arg(100).arg(3.14).arg("abc");

qDebug()<<s3;

8、QStringList

原文地址:https://www.cnblogs.com/PXYZ/p/13531043.html