QVariantList与vector<QString>之间的转换

1. vector<QString> 转换为 QVariant

如果你需要将 vector<QString> 转换为 QVariant,可以使用 QVariantList 来存储 QString 的列表,然后将其传递给 QVariant

#include <QVariant>
#include <QVariantList>
#include <QString>
#include <vector>

std::vector<QString> myStrings = {"Hello", "World"};
QVariantList myVariantList;
for (const QString &str : myStrings) {
    myVariantList.append(str);
}
QVariant myVariant(myVariantList);
2. QVariantQVariantList 转换回 vector<QString>

如果你有一个 QVariantQVariantList,并希望将其转换回 vector<QString>,可以这样操作:

QVariant myVariant; // 假设这是你的QVariant
if (myVariant.type() == QVariant::List) {
    QVariantList myVariantList = myVariant.toList();
    std::vector<QString> myStrings;
    for (const QVariant &var : myVariantList) {
        if (var.canConvert<QString>()) {
            myStrings.push_back(var.toString());
        }
    }
}
``` class TestData : public QObject { Q_OBJECT public: TestData(QObject *parent = nullptr) : QObject(parent) {} //关键 必须这么写 TestData(const TestData &other) : QObject(other.parent()) { //this = other; // Copy other data members here this->Barcode = other.Barcode; this->CameraCounts = other.CameraCounts; this->GroupDefine = other.GroupDefine; this->Basic = other.Basic; this->Result = other.Result; this->Normal = other.Normal; this->Defocus = other.Defocus; this->m_bBarcode = other.m_bBarcode; this->m_bCameraCounts = other.m_bCameraCounts; this->m_bDefocus = other.m_bDefocus; this->m_bGroupDefine = other.m_bGroupDefine; } //Person &operator=(const Person &other) = delete; // 禁用赋值运算符 //关键 必须这么写 TestData &operator=(const TestData &other) { if (this != &other) { //this = other; // Copy other data members here this->Barcode = other.Barcode; this->CameraCounts = other.CameraCounts; this->GroupDefine = other.GroupDefine; this->Basic = other.Basic; this->Result = other.Result; this->Normal = other.Normal; this->Defocus = other.Defocus; this->m_bBarcode = other.m_bBarcode; this->m_bCameraCounts = other.m_bCameraCounts; this->m_bDefocus = other.m_bDefocus; this->m_bGroupDefine = other.m_bGroupDefine; } return *this; } public: QString Barcode; QString CameraCounts; QVector<QString> GroupDefine; BasicData Basic; ResultData Result; NormalData Normal; DefocusData Defocus; bool m_bBarcode; bool m_bCameraCounts; bool m_bGroupDefine; bool m_bDefocus; public: QVariantMap toVariantMap() const { QVariantMap map; if (m_bBarcode) { map["Barcode"] = QVariant::fromValue(Barcode); } if (m_bCameraCounts) { map["CameraCounts"] = QVariant::fromValue(CameraCounts); } if (m_bGroupDefine) { QVariantList list; for (auto item : GroupDefine) { list.append(item); } map["GroupDefine"] = list; } if (m_bDefocus) { QVariantMap tempDefocusMap = Defocus.toVariantMap(); if (tempDefocusMap.size() > 0) { map["Defocus"] = tempDefocusMap; } } QVariantMap tempBasicMap = Basic.toVariantMap(); if (tempBasicMap.size()>0) { map["Basic"] = tempBasicMap; } QVariantMap tempNormalMap = Normal.toVariantMap(); if (tempNormalMap.size() > 0) { map["Normal"] = tempNormalMap; } QVariantMap tempResultMap = Result.toVariantMap(); if (tempResultMap.size() > 0) { map["Result"] = tempResultMap; }```改用MFC
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值