QVariant:可以接收int,double,QDatetime,QPoint,QMap,QList...多种数据类型,便是和boost:variant不太一样,它并不是一个模板类,一次只能接收一个数据
#include<QVariant>
#include<iostream>
#include<QDateTime>
#include<QPoint>
using namespace std;
int main()
{
QVariant v = 10;
int i = v.toInt();
cout << i << endl;
v = "abc";
cout << v.toString().toStdString() << endl;
v.setValue(20);
cout << v.toDouble() << endl;
cout << v.canConvert<int>()<<endl;
QDateTime dt;
cout << v.canConvert<QDateTime>()<<endl;//0
cout << dt.currentDateTime().toLocalTime().toString("yyyy.MM.dd").toStdString()<<endl;
v.setValue(dt);//equals to v = dt;
if(v.type()==QVariant::DateTime){
cout << v.toDateTime().currentDateTime().toLocalTime().toString("yyyy.MM.dd").toStdString() << endl;
}
cout << v.canConvert<QDateTime>()<<endl;//1
}
10
abc
20
1
0
2011.07.02
2011.07.02
1