日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt文档阅读笔记-QVariant::value()与qvariant_cast解析及使用

發布時間:2025/3/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt文档阅读笔记-QVariant::value()与qvariant_cast解析及使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

官方解析

博主栗子

QVariant::value()的小栗子

qvariant_cast小栗子

QVariant在容器中的使用


官方解析

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(); }

?

總結

以上是生活随笔為你收集整理的Qt文档阅读笔记-QVariant::value()与qvariant_cast解析及使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。