Qt字符串类应用与常用基本数据类型

简介: Qt字符串类应用与常用基本数据类型

一.Qt 字符串类应用

1、常用操作字符串

1.1 QString 提供一个二元的“+”操作符,主要用于组合两个字符串。QString str1'"Hello world"传递给 QString 一个 const char*类型的 ASCII 字符串“Hello world”,它被解释为一个典型的以“\0”结尾的 C 类型字符串

#include <QCoreApplication> //Qt提供一个事件循环
 
#include <QDebug> //输出流
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
 
    //1.1 QString 提供二元+操作符运用,功能与+=一样
    QString str1 = "zgl";
    str1 = str1+"nb";
    qDebug()<<str1; //打印信息
    qDebug()<<qPrintable(str1); //去双引号
 
    QString str2 = "666";
    str2 =  str2 +"zgl";
    qDebug()<<str2;
    qDebug()<<qPrintable(str2);
 
    return a.exec();
}
1.2 QString::append()函数具备与“+=”操作符同样的功能,直接在一个字符串末尾添加另一个字符串。
    //1.2 QString::append()函数
    QString str1 = "zgl";
    QString str2 = "nb";
    str1.append(str2);
    qDebug()<<str1;
    qDebug()<<qPrintable(str1);
1.3 组合字符串: QString::sprintf ()。其实它跟 C++库当中sprintf()函数一样
    //1.3 QString::sprintf()函数
    QString strtemp;
    strtemp.sprintf("%s","hello zgl");
    qDebug()<<qPrintable(strtemp);
    strtemp.sprintf("%s","hello ljx");
    qDebug()<<qPrintable(strtemp);
 
    strtemp.sprintf("%s %s","zgl","ljx"); //空格bian表示字符串间距
    qDebug()<<qPrintable(strtemp);
1.4 字符串组合方式 QString::arg()函数,该函数的重载可以处理多种数据类型。因为它类型安全,同时支持 Unicode,可以改变%n 参数顺序。
    //1.4 QString::arg()函数
    QString strtemp;
    strtemp = QString("%1 very nb and %2").arg("zgl").arg(666);
    qDebug()<<strtemp;
    qDebug()<<qPrintable(strtemp);
2、常用查询字符串

2.1 函数 QString::startswith()判断一个字符串是否以某个字符串开头。Qt::CaseInsensitive 代表大小写不敏感; Qt::CaseSensitive表示大小定敏感。对应关系函数:QString::endsWith()。

    //2.1 QString::startswith()函数
    QString strtemp = "zgl very nb";
    qDebug()<<strtemp.startsWith("zgl",Qt::CaseSensitive); //大小写敏感,返回值为true
    qDebug()<<strtemp.startsWith("Zgl",Qt::CaseSensitive); //大小写敏感,返回值为false
    qDebug()<<strtemp.startsWith("Zgl",Qt::CaseInsensitive); //大小写不敏感,返回值为true
    qDebug()<<strtemp.startsWith("nb",Qt::CaseInsensitive); //大小写不敏感,返回值为false
2.2 函数 QString::contains (判断一个指定的字符串是否出现过)
    //2.2 QString::contains()函数
    QString strtemp = "zgl nb";
    qDebug()<<strtemp.contains("zgl",Qt::CaseSensitive); //返回值为true
2.3 QString::toInt()函数将字符串转换为整型数值   toDouble () /toFloat () /toLong()等等
    //2.3 QString::toInt()函数
    QString str="25";
    bool isloop; //bool类型返回值为true或者false
    int hex = str.toInt(&isloop,16); //十六进制的25转换为十进制为37
    qDebug()<<"isloop="<<isloop<<","<<"hex="<<hex<<endl;
2.4 QString::compare ( 函数对两个字符串进行比较)
    //2.4 QString::compare()函数
    int a1 = QString::compare("abc","Abc",Qt::CaseInsensitive); //大小写不敏感
    int b1 = QString::compare("abc","Abc",Qt::CaseSensitive); //大小写敏感
    int c1 = QString::compare("Abc","abc",Qt::CaseSensitive); //大小写敏感
    cout<<"a = "<<a1<<","<<"b = "<<b1<<","<<"c = "<<c1<<endl; //ASCII码大小写差32,A=65,a=97
2.5 Qt将QString 转换成 ASCII码
    //2.5 Qt将QString 转换成 ASCII码 ASCII码大小写差32,A=65,a=97
    QString str = "ABC abc";
    QByteArray bytes = str.toUtf8();
    for(int i=0;i<str.size();i++){
        qDebug()<<int(bytes.at(i));
    }

二、Qt 常见基本数据类型(注意:定义在#include <QtGlobal>)

QT基本数据类型定义在#include <QtGlobal> 中,QT基本数据类型有:

类型名称 注释 备注
qint8 signed char 有符号8位数据
qint16 signed short 16位数据类型
qint32 signed short 32位有符号数据类型
qint64 long long int 或(__int64) 64位有符号数据类型,Windows中定义为__int64
qintptr qint32 或 qint64 指针类型 根据系统类型不同而不同,32位系统为qint32、64位系统为qint64
qlonglong long long int 或(__int64) Windows中定义为__int64
qptrdiff qint32 或 qint64 根据系统类型不同而不同,32位系统为qint32、64位系统为qint64
qreal double 或 float 除非配置了-qreal float选项,否则默认为double
quint8 unsigned char 无符号8位数据类型
quint16 unsigned short 无符号16位数据类型
quint32 unsigned int 无符号32位数据类型
quint64 unsigned long long int 或 (unsigned __int64) 无符号64比特数据类型,Windows中定义为unsigned __int64
quintptr quint32 或 quint64 根据系统类型不同而不同,32位系统为quint32、64位系统为quint64
qulonglong unsigned long long int 或 (unsigned __int64) Windows中定义为__int64
uchar unsigned char 无符号字符类型
uint unsigned int 无符号整型
ulong unsigned long 无符号长整型
ushort unsigned short 无符号短整型


QDateTime
// 构造函数
QDateTime::QDateTime();
QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec = Qt::LocalTime);
// 公共成员函数
// 设置日期
void QDateTime::setDate(const QDate &date);
// 设置时间
void QDateTime::setTime(const QTime &time);
// 给当前日期对象追加 年/月/日/秒/毫秒, 参数可以是负数
QDateTime QDateTime::addYears(int nyears) const;
QDateTime QDateTime::addMonths(int nmonths) const;
QDateTime QDateTime::addDays(qint64 ndays) const;
QDateTime QDateTime::addSecs(qint64 s) const;
QDateTime QDateTime::addMSecs(qint64 msecs) const;
// 得到对象中的日期
QDate QDateTime::date() const;
// 得到对象中的时间
QTime QDateTime::time() const;
// 日期和时间格式, 格式字符参考QDate 和 QTime 类的 toString() 函数
QString QDateTime::toString(const QString &format) const;
// 操作符重载 ==> 日期时间对象的比较
bool QDateTime::operator!=(const QDateTime &other) const;
bool QDateTime::operator<(const QDateTime &other) const;
bool QDateTime::operator<=(const QDateTime &other) const;
bool QDateTime::operator==(const QDateTime &other) const;
bool QDateTime::operator>(const QDateTime &other) const;
bool QDateTime::operator>=(const QDateTime &other) const;
// 静态函数
// 得到当前时区的日期和时间(本地设置的时区对应的日期和时间)
[static] QDateTime QDateTime::currentDateTime();
QByteArray
    // QDateTime QByteArray
    QDateTime dt;
    QString strDT=dt.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    qDebug()<<strDT<<endl;
 
    QByteArray a1("Qt Creator Hello World.");
    QByteArray b1=a1.toLower(); // 将字符串大写字母转小写,小写不变
    qDebug()<<b1<<endl;
    QByteArray c1=a1.toUpper();
    qDebug()<<c1<<endl;
相关文章
|
2月前
|
存储 容器
Qt中 QMap 类、QHash 类、QVector 类详解
Qt中 QMap 类、QHash 类、QVector 类详解
Qt中 QMap 类、QHash 类、QVector 类详解
|
2月前
|
开发者
Qt 布局(QSplitter 类&QDockWidget 类) 总结
Qt 布局(QSplitter 类&QDockWidget 类) 总结
Qt 布局(QSplitter 类&QDockWidget 类) 总结
|
21天前
使用QMovie类来实现在QT中的动态加载效果。
使用QMovie类来实现在QT中的动态加载效果。
21 0
|
2月前
|
C++ 存储 容器
|
2月前
|
Windows
Qt 目录操作(QDir 类)及展示系统文件实战 & QFilelnfo 类介绍和获取文件属性项目实战
Qt 目录操作(QDir 类)及展示系统文件实战 & QFilelnfo 类介绍和获取文件属性项目实战
Qt 目录操作(QDir 类)及展示系统文件实战 & QFilelnfo 类介绍和获取文件属性项目实战
|
1月前
|
API
【Qt】Qt定时器类QTimer
【Qt】Qt定时器类QTimer
|
2月前
|
C++
【qt】自定义代理类
【qt】自定义代理类
13 0
|
2月前
|
搜索推荐
【qt】自定义界面类
【qt】自定义界面类
20 0
|
2月前
|
C语言 容器
【qt】QString字符串2
【qt】QString字符串2
14 0