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

QT中的元对象系统(一):QVariant的简单说明QVariant 可以表示 QT 中的大部分类型 它和 pascal 中的 variant 类型或 c 中的 void 类型有点相似 不过它的使用和 c 中的 union 类似 其实现也是用 union 在 qvariant h 头文件中 我们可以看到这样

大家好,欢迎来到IT知识分享网。
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()确定是否可以转化。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/131792.html

(0)
上一篇 2025-08-05 16:15
下一篇 2025-08-05 16:20

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信