Qt文档阅读笔记-QVariant::value()与qvariant_cast解析及使用
目錄
官方解析
博主栗子
QVariant::value()的小栗子
qvariant_cast小栗子
QVariant在容器中的使用
官方解析
QVariant::value()與qvariant_cast
qvariant_cast<T>(const QVariant &value)
把value轉(zhuǎn)化為T,這個(gè)函數(shù)等同于QVariant::value()
下面來解釋下T QVariant::value()
把QVariant中的值轉(zhuǎn)化為數(shù)據(jù)T??梢酝ㄟ^調(diào)用canConvert()這個(gè)函數(shù)來判斷,他是否可以轉(zhuǎn)化。如果不用canConvert()進(jìn)行判斷,強(qiáng)制進(jìn)行轉(zhuǎn),那么如果轉(zhuǎn)換失敗,就返回要T的默認(rèn)構(gòu)造函數(shù)的值;
如果支持轉(zhuǎn)化為T,那么這個(gè)函數(shù)就和toString(),toInt()這些一個(gè)吊樣了!
如果這個(gè)QVariant包含一個(gè)指針,這個(gè)指針是QObject的派生類,那么他的任一類型都可以轉(zhuǎn)化成Object的指針。如果存儲(chǔ)在QVariant中的數(shù)據(jù)能使用qobject_cast轉(zhuǎn)化為T,那么將會(huì)返回T這個(gè)指針,否則返回null,注意想讓QObject的派生類正常工作都要帶Q_OBJECT宏。
如果QVariant包含連續(xù)的內(nèi)容,比如容器的元素都將被轉(zhuǎn)化為QVariants,并且返回QVariantList。
?
博主栗子
QVariant::value()的小栗子
程序運(yùn)行截圖如下:
源碼如下:
#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小栗子
運(yùn)行截圖如下:
源碼如下:
#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在容器中的使用
運(yùn)行截圖如下:
源碼如下:
#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(); }?
總結(jié)
以上是生活随笔為你收集整理的Qt文档阅读笔记-QVariant::value()与qvariant_cast解析及使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-仿QQ登录界面(换肤,切换
- 下一篇: Qt工作笔记-第一个QML(QQuick