第2章:QVariant

1,QVariant类,类似于C++的联合数据类型,能够保存很多Qt类型的值包括QColor,QBrush等

2,源码:

    //QVariant类类似于C++的联合(union)数据类型,能够保存很多Qt类型的值,包括QColor, QBrush等
    //也能够存放Qt的容器类型的值

    QVariant V(709);                      //声明一个变量并初始化为int
    QVariant W("How are You!");           //声明一个变量并初始化为一个字符串

    QMap<QString, QVariant> map;
    map["int"]     = 709;
    map["double"]  = 709.709;
    map["string"]  = "How Are You!";
    map["color"]   = QColor(255, 0, 0);

    //qDebug() :调用相应的转换函数并输出
    qDebug() <<map["int"]    << map["int"].toInt();
    qDebug() <<map["double"] << map["double"].toDouble();
    qDebug() <<map["string"] << map["string"].toString();
    qDebug() <<map["color"]  << map["color"].value<QColor>();


    //QVariant::type枚举的变量
    //QVariant::Invalid 无效类型
    //QVariant::Region Bitmap Bool Brush Size Char Color Cursor Date DateTime Double Font Icon
    //Time Line Palette List SizePolicy String Map StringList Point Pen Pixmap Rect Image
    //QVariant::UserType 用户自定义类型

3,效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值