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

QT中的元对象系统(一):QVariant的简单说明 收藏
       原创文章,转载请注明出处,谢谢!       
       作者:清林,博客名:飞空静渡


       QVariant可以表示QT中的大部分类型,它和pascal中的variant类型或c中的void类型有点相似,不过它的使用和c中的union类 似,其实现也是用union,在qvariant.h头文件中,我们可以看到这样定义:

       view plaincopy to clipboardprint?
class Q_CORE_EXPORT QVariant  
{  
  public:  
     enum Type {  
         Invalid = 0,  
   
         Bool = 1,  
         Int = 2,  
         UInt = 3,  
         LongLong = 4,  
         ULongLong = 5,  
......  
        UserType = 127,  
 #ifdef QT3_SUPPORT  
         IconSet = Icon,  
         CString = ByteArray,  
         PointArray = Polygon,  
 #endif  
         LastType = 0xffffffff // need this so that gcc >= 3.4 allocates 32 bits for Type  
     };  
   
     inline QVariant();  
...............  

class Q_CORE_EXPORT QVariant
{
  public:
     enum Type {
         Invalid = 0,
 
         Bool = 1,
         Int = 2,
         UInt = 3,
         LongLong = 4,
         ULongLong = 5,
......
        UserType = 127,
 #ifdef QT3_SUPPORT
         IconSet = Icon,
         CString = ByteArray,
         PointArray = Polygon,
 #endif
         LastType = 0xffffffff // need this so that gcc >= 3.4 allocates 32 bits for Type
     };
 
     inline QVariant();
...............
}

 

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

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

基本的使用如下:

view plaincopy to clipboardprint?
QDataStream out(...);  
 QVariant v(123);                // The variant now contains an int  
 int x = v.toInt();              // x = 123  
 out << v;                       // Writes a type tag and an int to out  
 v = QVariant("hello");          // The variant now contains a QByteArray  
 v = QVariant(tr("hello"));      // The variant now contains a QString  
 int y = v.toInt();              // y = 0 since v cannot be converted to an int  
 QString s = v.toString();       // s = tr("hello")  (see QObject::tr())  
 out << v;                       // Writes a type tag and a QString to out  
 ...  
 QDataStream in(...);            // (opening the previously written stream)  
 in >> v;                        // Reads an Int variant  
 int z = v.toInt();              // z = 123  
 qDebug("Type is %s",            // prints "Type is int"  
         v.typeName());  
 v = v.toInt() + 100;            // The variant now hold the value 223  
 v = QVariant(QStringList()); 
QDataStream out(...);
 QVariant v(123);                // The variant now contains an int
 int x = v.toInt();              // x = 123
 out << v;                       // Writes a type tag and an int to out
 v = QVariant("hello");          // The variant now contains a QByteArray
 v = QVariant(tr("hello"));      // The variant now contains a QString
 int y = v.toInt();              // y = 0 since v cannot be converted to an int
 QString s = v.toString();       // s = tr("hello")  (see QObject::tr())
 out << v;                       // Writes a type tag and a QString to out
 ...
 QDataStream in(...);            // (opening the previously written stream)
 in >> v;                        // Reads an Int variant
 int z = v.toInt();              // z = 123
 qDebug("Type is %s",            // prints "Type is int"
         v.typeName());
 v = v.toInt() + 100;            // The variant now hold the value 223
 v = QVariant(QStringList());

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

view plaincopy to clipboardprint?
QVariant x, y(QString()), z(QString(""));  
x.convert(QVariant::Int);  
// x.isNull() == true  
// y.isNull() == true, z.isNull() == false  
// y.isEmpty() == true, z.isEmpty() == true 
 QVariant x, y(QString()), z(QString(""));
 x.convert(QVariant::Int);
 // x.isNull() == true
 // y.isNull() == true, z.isNull() == false
 // y.isEmpty() == true, z.isEmpty() == true


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

view plaincopy to clipboardprint?
QVariant variant;  
...  
QColor color = variant.value<QColor>(); 
 QVariant variant;
 ...
 QColor color = variant.value<QColor>();

相反的赋值就可以这样:

view plaincopy to clipboardprint?
QColor color = palette().background().color();  
QVariant variant = color; 
 QColor color = palette().background().color();
 QVariant variant = color;


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

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fjb2080/archive/2009/12/09/4972915.aspx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值