目录
官方解析
QVariant::value()与qvariant_cast
qvariant_cast<T>(const QVariant &value)
把value转化为T,这个函数等同于QVariant::value()
下面来解释下T QVariant::value()
把QVariant中的值转化为数据T。可以通过调用canConvert()这个函数来判断,他是否可以转化。如果不用canConvert()进行判断,强制进行转,那么如果转换失败,就返回要T的默认构造函数的值;
如果支持转化为T,那么这个函数就和toString(),toInt()这些一个吊样了!
QVariant v;
MyCustomStruct c;
if (v.canConvert<MyCustomStruct>())
c = v.value<MyCustomStruct>();
v = 7;
int i = v.value<int>(); // same as v.toInt()
QString s = v.value<QString>(); // same as v.toString(), s is now "7"
MyCustomStruct c2 = v.value<MyCustomStruct>(); // conversion failed, c2 is empty
如果这个QVariant包含一个指针,这个指针是QObject的派生类,那么他的任一类型都可以转化成Object的指针。如果存储在QVariant中的数据能使用qobject_cast转化为T,那么将会返回T这个指针,否则返回null,注意想让QObject的派生类正常工作都要带Q_OBJECT宏。
如果QVariant包含连续的内容,比如容器的元素都将被转化为QVariants,并且返回QVariantList。
QList<int> intList;
intList.push_back(7);
intList.push_back(11);
intList.push_back(42);
QVariant variant = QVariant::fromValue(intList);
if (variant.canConvert<QVariantList>()) {
QSequentialIterable iterable = variant.value<QSequentialIterable>();
// Can use foreach:
foreach (const QVariant &v, iterable) {
qDebug() << v;
}
// Can use C++11 range-for:
for (const QVariant &v : iterable) {
qDebug() << v;
}
// Can use iterators:
QSequentialIterable::const_iterator it = iterable.begin();
const QSequentialIterable::const_iterator end = iterable.end();
for ( ; it != end; ++it) {
qDebug() << *it;
}
}
博主栗子
QVariant::value()的小栗子
程序运行截图如下:
源码如下:
#include <QApplication>
#include <QVariant>
#include <QDebug>
struct MyData{
int valueA=100; //Using MinGW can be writing in this way. MSVC could not be in this way.
QString str="HelloWorld";
};
Q_DECLARE_METATYPE(MyData)
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyData myData;
int id=qRegisterMetaType<MyData>();
QVariant variant;
variant.setValue(myData);
if(variant.canConvert(id)){
qDebug()<<"it can be converted by this means!";
MyData tempData=variant.value<MyData>();
qDebug()<<tempData.valueA<<" "<<tempData.str;
}
variant=10;
int i=variant.value<int>();
qDebug()<<"int i is "<<i;
QString s=variant.value<QString>();
qDebug()<<"QString s is "<<s;
return a.exec();
}
qvariant_cast小栗子
运行截图如下:
源码如下:
#include <QApplication>
#include <QVariant>
#include <QDebug>
struct MyData{
int valueA=100; //Using MinGW can be writing in this way. MSVC could not be in this way.
QString str="HelloWorld";
};
Q_DECLARE_METATYPE(MyData)
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyData myData;
QVariant variant;
variant.setValue(myData);
MyData tempData=qvariant_cast<MyData>(variant);
qDebug()<<tempData.valueA<<" "<<tempData.str;
return a.exec();
}
QVariant在容器中的使用
运行截图如下:
源码如下:
#include <QApplication>
#include <QVariant>
#include <QList>
#include <QObject>
#include <QDebug>
#include "mydata.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyData *myData=new MyData;
QVariant variant;
variant.setValue(myData);
MyData *tempData=qvariant_cast<MyData*>(variant);
qDebug()<<tempData->m_value<<" "<<tempData->m_str;
QList<MyData*> myDataList;
myDataList.push_back(new MyData(NULL,1,"first"));
myDataList.push_back(new MyData(NULL,2,"second"));
myDataList.push_back(new MyData(NULL,3,"third"));
QVariant tempVar=QVariant::fromValue(myDataList);
if(tempVar.canConvert<QVariantList>()){
qDebug()<<"successful!";
QSequentialIterable iterable=tempVar.value<QSequentialIterable>();
qDebug()<<endl<<"foreach";
foreach(const QVariant &v,iterable){
qDebug()<<v.value<MyData*>()->m_value<<" "<<v.value<MyData*>()->m_str;
}
qDebug()<<endl<<"for";
for(const QVariant &v:iterable){
qDebug()<<v.value<MyData*>()->m_value<<" "<<v.value<MyData*>()->m_str;
}
qDebug()<<endl<<"iterators";
QSequentialIterable::const_iterator it=iterable.begin();
for(;it!=iterable.end();it++){
qDebug()<<(*it).value<MyData*>()->m_value<<" "<<(*it).value<MyData*>()->m_str;
}
}
return a.exec();
}