QT中的元对象系统(一):QVariant的简单说明

QVariant可以表示QT中的大部分类型,它和pascal中的variant类型或c中的void类型有点相似, 不过它的使用和c中的union类似,其实现也是用union ,在qvariant.h头文件中,我们可以看到这样定义:
[cpp]  view plain copy
  1. class Q_CORE_EXPORT QVariant  
  2. {  
  3.   public:  
  4.      enum Type {  
  5.          Invalid = 0,  
  6.    
  7.          Bool = 1,  
  8.          Int = 2,  
  9.          UInt = 3,  
  10.          LongLong = 4,  
  11.          ULongLong = 5,  
  12. ......  
  13.         UserType = 127,  
  14.  #ifdef QT3_SUPPORT  
  15.          IconSet = Icon,  
  16.          CString = ByteArray,  
  17.          PointArray = Polygon,  
  18.  #endif  
  19.          LastType = 0xffffffff // need this so that gcc >= 3.4 allocates 32 bits for Type  
  20.      };  
  21.    
  22.      inline QVariant();  
  23. ...............  
  24. }  

我们可以用type()成员函数来返回它的类型:Type QVariant::type () const

为了取得它的值,我们可以用类似的toT()这样的函数来取得,如toInt()、toString()等。

基本的使用如下:

 
[cpp]  view plain copy
  1. QDataStream out(...);  
  2.  QVariant v(123);                // The variant now contains an int  
  3.  int x = v.toInt();              // x = 123  
  4.  out << v;                       // Writes a type tag and an int to out  
  5.  v = QVariant("hello");          // The variant now contains a QByteArray  
  6.  v = QVariant(tr("hello"));      // The variant now contains a QString  
  7.  int y = v.toInt();              // y = 0 since v cannot be converted to an int  
  8.  QString s = v.toString();       // s = tr("hello")  (see QObject::tr())  
  9.  out << v;                       // Writes a type tag and a QString to out  
  10.  ...  
  11.  QDataStream in(...);            // (opening the previously written stream)  
  12.  in >> v;                        // Reads an Int variant  
  13.  int z = v.toInt();              // z = 123  
  14.  qDebug("Type is %s",            // prints "Type is int"  
  15.          v.typeName());  
  16.  v = v.toInt() + 100;            // The variant now hold the value 223  
  17.  v = QVariant(QStringList());  

QViriant支持空值的使用,你可以定义一个空的QViriant,并在后面才给它赋值。

 
[cpp]  view plain copy
  1. QVariant x, y(QString()), z(QString(""));  
  2. x.convert(QVariant::Int);  
  3. // x.isNull() == true  
  4. // y.isNull() == true, z.isNull() == false  
  5. // y.isEmpty() == true, z.isEmpty() == true  

QVariant是定义在QtCore库中的,前面说的qvariant.h就在QtCore目录下,因此它不提供类似于toInt()、toString()这样的函数去转化QtGui中的类型,如QColor、QImage和QPixmap等。但你可以使用QVariant::value()或者qVariantValue()模板函数转化。

 
[cpp]  view plain copy
  1. QVariant variant;  
  2. ...  
  3. QColor color = variant.value<QColor>();  

相反的赋值就可以这样:

[cpp]  view plain copy
  1. QColor color = palette().background().color();  
  2. QVariant variant = color;  

 

你可以使用canConvert()确定是否可以转化。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值