Qt学习笔记#第三章:常见的数据类型

1.QString(字符串)
 //1、qstring 提供二元“+”操作应用 ,功能一样 +=
    QString str1 = "Ling";
    str1 = str1 + "sheng";
    qDebug() << str1; //打印信息
    qDebug()<<qPrintable(str1); //去掉双引号

    QString str2 = "123456";
    str2 += "ABCDE";
    qDebug()<<qPrintable(str2); //去掉双引号


    //2、append()  后面追加字符串
    str2.append("G");
    qDebug()<<qPrintable(str2); //去掉双引号

    //3、sprintf() 组合 格式化输出 和c库中的作用一样
    QString tmp;
    str2.sprintf("%s","hello world");
    qDebug()<<qPrintable(str2);//hello world

    tmp.sprintf("%s %s","str2","str1");
    qDebug()<<qPrintable(tmp); //str2 str1

    //4、QString::arg()函数
    QString str4;
    str4 = QString("%1 was born in %2.").arg("sunny").arg(2000);
    qDebug()<<qPrintable(str4);


    //5、字符串操作查询  QString::startsWith()函数 是否以什么字符串开头
    //Qt::CaseInsensitive 大小写不敏感   Qt::CaseSensitive 大小写敏感
    //6、对应关系  以什么结尾QString::endsWith()
    QString str5 = "How are you";
    qDebug()<<str5.startsWith("How",Qt::CaseSensitive);
    qDebug()<<str5.startsWith("how",Qt::CaseSensitive);
    qDebug()<<str5.startsWith("how",Qt::CaseInsensitive);
    qDebug()<<str5.endsWith("You",Qt::CaseInsensitive);

    //7、contains() 判断一个字符是否出现过
    QString str7 = "How are you";
    qDebug()<<str7.contains("How",Qt::CaseSensitive);

    //8、toInt() 将字符串转换为整形数值
    QString str8 = "25";
    qDebug()<<str8.toInt();


    //9、compare() 比较两个字符串 相等 0  1
    QString str9 = "xxx";
    QString str10 = "XXX";
    qDebug()<<str9.compare(str10,Qt::CaseInsensitive);//大小写不敏感
    qDebug()<<str9.compare(str10,Qt::CaseSensitive);
    qDebug()<<QString::compare(str9,str10,Qt::CaseInsensitive);
    qDebug()<<QString::compare(str9,str10,Qt::CaseInsensitive);

    qDebug()<<QString::compare("abcd","ABCD",Qt::CaseInsensitive);


    //10、将QString 转换为ASCII
    QString str11 = "ABC abc";
    QByteArray bytes = str11.toUtf8();
    for(int i =0;i<str11.size();i++)
        qDebug()<<int(bytes.at(i));

    //11、QDataTime QByteArray
    //获取当前时间
    QDateTime dt;
    QString strDt = dt.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    qDebug()<<strDt<<endl;

    QByteArray a1("QT Creat Hello World;");
    QByteArray b1 = a1.toLower(); //将字符串大写字母变为小写字母 小写字母不变
    qDebug()<<b1<<endl;
    QByteArray c1 = b1.toUpper(); //小写变大写
    qDebug()<<c1<<endl;
2.QMap(键值对顺序存储,QMultiMap键值可以重复)
//QMap 键值对 按照键值顺序存储
    //1、创建QMap实例,第一个参数随便取   第二个参数为T
    QMap<QString,int> map;

    //插入信息
    map["Chinese"] =100;
    map.insert("English",120);
    map.insert("Math",130);
    map.insert("Physe",110);
    qDebug()<<map<<endl;
//    for(auto it : map)
//        qDebug()<<it<<endl;

    //删除数据
    map.erase(map.begin());
    map.remove("Math"); //根据键值删除数据
    qDebug()<<map<<endl;

    //遍历QMap类的实例:数据信息
    //1、迭代器(java 类型的迭代器)
    QMapIterator<QString,int> itr(map);
    while (itr.hasNext())
    {
        itr.next();
        qDebug()<<itr.key()<<itr.value();
    }
    //2、STL类型迭代
    for(QMap<QString,int>::const_iterator stritr = map.constBegin();stritr != map.constEnd();stritr++)
    {
        qDebug()<<stritr.key()<<stritr.value();
    }

    //key值查找   value查找
    qDebug()<<"根据key值查找对应的键值"<<map.value("Physe");
    qDebug()<<"根据value值查找键值:"<<map.key(110);

    //修改值  key-value 再次调用insert 函数值将覆盖原有的值
    map.insert("Physe",112);
    qDebug()<<"根据key值查找对应的键值"<<map.value("Physe");


    //查询是否包含某一个键值
    qDebug()<<map.contains("Physe");

    //输出所有QMap实例化:key键和T键值
    QList<QString> aKeys = map.keys();
    qDebug()<<aKeys;
    QList<int> aValues = map.values();
    qDebug()<<aValues;

    //一个键对应多个值
    //直接使用QMultiMap类实例化一个QMap对象
    QMultiMap<QString,QString> mulmap;
    mulmap.insert("student","no");
    mulmap.insert("student","name");
    mulmap.insert("student","age");
    mulmap.insert("student","high");
    mulmap.insert("student","weight");
    qDebug()<<mulmap;//从输出的结果看出mulmap仍然是一个QMap对象
3.QHash(相比QMap查找速度快,它是任意顺序存储,维护一张hash表,当数据的顺序无关紧要,建议使用)
    //QHash类   任意顺序存储 维护hash表
    QHash<QString,int> qhash;
    qhash["key 1"] = 3;
    qhash["key 1"] = 8;
    qhash["key 4"] = 4;
    qhash["key 2"] = 2;
    qhash.insert("key3",3);

    for(auto it = qhash.constBegin();it!=qhash.constEnd();it++)
    {
        qDebug()<< it.key()<<" "<<it.value();
    }
    //输出
    QList<QString> list = qhash.keys();
    qDebug()<<qhash.value(list[0]);//输出值
    // 检查键是否存在
    bool containsBob = hash.contains("Bob");
    qDebug() << "Contains Bob:" << containsBob; // 输出: "Contains Bob: true"
    
    // 删除键值对
    hash.remove("Charlie");
4.QVector(相邻内存存储给定的数据类型,在前部和中间插入数据很慢,因为有大量数据被移动,这是由QVector存储方式决定的)
 //QVector类 相邻内存中存储数据 操作数据大量被移动
    QVector<int> qv;
    qv<<10;
    qv<<20;
    qv.append(30);
    qv.append(40);
    qv.insert(4,50);
    qDebug()<<qv;
    //求出元素的个数
    qDebug()<<qv.count();
    //删除元素
    qv.remove(3);
    qDebug()<<qv;
    //删除范围左闭右开[)
    qv.remove(0,2);
    qDebug()<<qv;
    qDebug()<<qv.contains(40);
5.QList(一个动态数组,用于存储一个列表的元素;QList<T> T 是指针或者指针大小的数据类型,存储在数组中;如果存储对象指针,则该指针指向实际存储的对象)
6.QLinkList(链式列表 非连续内存保存数据;不能使用下标,只能用迭代器访问数据;插入效率高)
 //QList类   存储指针类型或者指针大小类型的数据  是数组存储
    QList<int> qlist; //初始化一个空的QList《int》链表
    for(int i = 0;i <10;i++)
    {
        qlist.insert(qlist.end(),i+10);
    }

    //通过QList<int>::iterator 读写迭代器
    for( QList<int>::iterator x = qlist.begin(); x!= qlist.end(); x++)
    {
        qDebug() << *x;
        *x = (*x)*10+6;
    }
    for(auto it = qlist.constBegin();it!= qlist.constEnd();it++)
    {
        qDebug()<<*it;
    }

    //添加元素
    qlist.append(666);
    for(auto it = qlist.constBegin();it!= qlist.constEnd();it++)
    {
        qDebug()<<*it;
    }

    //查询 元素  和元素个数
    qDebug()<<qlist.at(3);//第三个元素是多少
    qDebug()<<qlist.contains(666);//有没有这个元素
    qDebug()<<qlist.count();//元素多少个

    //修改元素
    qlist.replace(10,888);
    qDebug()<<qlist;
    //删除元素
    qDebug()<<endl;
    qlist.removeAt(0); // 删除下标为0的元素 其余元素向前偏移
    qDebug()<<qlist;
    qlist.removeLast();//删除最后一个元素
    qDebug()<<qlist;
    qlist.removeOne(116);//删除 列表第一次出现的数值
    qDebug()<<qlist;


    //==================================================
    //QLinkLedist  双向链表是存储  (不能通过索引方式访问元素)链表 保存大规模数据 建议使用
    QLinkedList<QString> qlinklist;
    for(int i = 1;i<13;i++)
        qlinklist<<QString("%1 %2").arg("Month:").arg(i);
    /*
        qlinklist<<   向链表插入数据
        QString("%1 %2").arg("Month:").arg(i)  填充字符串参数
    */
    for(auto it = qlinklist.begin();it != qlinklist.end();it++)
    {
        qDebug()<< *it;
    }
7.QVariant

QVariant 类是 Qt 框架中的一个通用数据容器类。它可以用于存储和操作各种不同类型的数据,例如整数、浮点数、字符串、日期等。QVariant 提供了一种统一的方式来处理和传递不同类型的数据,特别在框架内部和跨模块之间的数据传输非常有用。

以下是 QVariant 类的主要特点和功能:

  1. 存储不同类型的数据:QVariant 可以存储任意类型的数据,包括基本数据类型(如 int、float、double、bool),Qt 的类(如 QString、QDateTime、QPen、QColor、QIcon、QPixmap等)以及自定义的用户类型(通过 QVariant 的注册机制)。

  2. 自动类型转换:QVariant 可以自动进行数据类型转换,从而实现不同类型数据之间的转换。例如,可以将一个整数存储在 QVariant 中,然后按需将其转换为字符串或者浮点数等其他类型。

  3. 扩展性和可扩展类型:开发者可以通过 QVariant 的注册机制扩展支持自定义的数据类型。这样,QVariant 将能够适应更多种类的数据,并且能够正确地执行数据类型转换。

  4. 查询和判断数据类型:可以使用 QVariant 的类型相关方法(如 type() 和 typeName())来查询 QVariant 对象中存储的数据的类型信息。此外,可以使用 QVariant 的 isNull() 方法检查 QVariant 对象是否为空。

  5. 值的提取和设置:可以使用 QVariant 的 value() 方法获取存储在 QVariant 对象中的具体数值,同时也可以使用 setValue() 方法将新的值设置到 QVariant 对象中。

  6. 信号与槽的传递:QVariant 可以用于在信号与槽之间传递参数。它提供了一种灵活的方式来处理不同类型的参数,使得信号与槽的连接更加方便和通用。

 //创建字符串列表:QStringList
    qDebug()<<endl;
    QStringList qls;
    qls<<"A"<<"B"<<"C"<<"D";

    QVariant qv(qls); //将列表存取在一个QVariant变量中
    if(qv.type() == QVariant::StringList)
    {
        QStringList qlist = qv.toStringList();
        for (int i = 0;i<qlist.size();i++)
        {
            qDebug()<<qlist.at(i);
        }
    }

    //应用结构体类型和QVariant类配合使用
    qDebug()<<endl;
    Student stu;
    stu.iNo = 202201;
    stu.strName= "sunny";
    stu.score = 715;

    // 使用静态方法保存数据
    QVariant qstu = QVariant::fromValue(stu);
    if(qstu.canConvert<Student>()) //判断是否可以转换操作
    {
        Student stuValue = qstu.value<Student>();  //获取数据
        Student qtmp = qvariant_cast<Student>(qstu);//获取数据
        qDebug()<<stuValue.iNo<<stuValue.strName<<stuValue.score;
        qDebug()<<qtmp.iNo<<qtmp.strName<<qtmp.score;
    }
8.其他常见的数据类型

1. **QByteArray**:用于处理字节数据的类,支持二进制数据操作。

2. **QSet**:一个集合类,存储唯一的元素,不允许重复。

3. **QDate** 和 **QTime**:分别用于处理日期和时间。

4. **QDateTime**:用于处理日期和时间的组合。

5. **QColor**:用于表示颜色。

6. **QPixmap** 和 **QImage**:用于处理图像数据。

7. **QPoint** 和 **QPointF**:用于表示二维坐标点。

8. **QSize** 和 **QSizeF**:用于表示二维尺寸。

9. **QRect** 和 **QRectF**:用于表示矩形区域。

10. **QRegExp** 和 **QRegularExpression**:用于正则表达式匹配。

11. **QUrl**:用于处理统一资源定位符 (URL)。

12. **QIODevice** 和其派生类:用于处理输入/输出设备,如文件、网络套接字等。

13. **QDataStream**:用于序列化和反序列化数据。

14. **QModelIndex** 和 **QItemSelection**:用于在模型-视图框架中表示数据项和选择。

具体可以查阅Qt官方文档。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值