嵌套的QVariantMap

时间:2014-07-10 15:42:21

标签: c++ qt nested qvariant

我使用嵌套的QVariantMap,并且在定义采用路径(字符串列表)的方法时遇到问题,并将指针返回到低级QVariantMaps:

QVariantMap * getQVariantMap( QStringList spec) const {
    QVariantMap * deep_p = this->hash_p;

    for( QStringList::const_iterator it = spec.begin();
                                     it!= spec.end();
                                   ++it)
    {
        QVariantMap::const_iterator i = hash_p->find( *it);
        if( i == hash_p->end())
            return 0x00;

        res_p = & i.value().toMap();
    }

    return res_p;
}

...

QVariantMap map, level1, level2;
level2["L2"] = "bazzinga!";

level1["L1"] = QVariant::fromValue( level2);
level1["foo"] = "foo";

map["L0"] = QVariant::fromValue( level1);
map["foo"] = "foo";

QStringList qsl;
            qsl.append( "L1");
            qsl.append( "L0");

QVariantMap * qvm = getQVariantMap( qsl);//pointer to level2 part of the nested QVariantMap.

qvm[ "foo"] = "baz";

//map[ "L0"].toMap()[ "L1"].toMap()[ "foo"] == "baz";

...

我收到以下错误:

taking address of temporary [-fpermissive]

我知道为什么。问题是我需要获得指向嵌套结构部分的指针,但我无法引用QVariant 的内部。我尝试使用post Assigning to nested QVariantMap中建议的data_ptr,但没有运气。由于显而易见的原因,我不想在插入/删除/值上做出特殊的包装(为什么,据我所知,在链接主题中接受的答案不是我的情况)。

我试过了:

  • 将QPointer存储在QVariant中 - 没有运气,因为QVariant不是QObject;
  • 使用QVariant :: fromValue(QVariantMap *)存储指针,但是当我调用它时,它会返回QVariantMap而不是指针,所以我再次得到了临时错误地址。无论哪种方式,我都不认为这就是我想要的。

某人应该如何正确地做到这一点?

0 个答案:

没有答案