简体   繁体   English

使用字符串在Qt中创建QVariantMap

[英]Creating a QVariantMap in Qt with strings

Apologies for asking something this trivial but I just can't seem to get it right so I guess I've completely misunderstood everything I thought I knew about memory management. 很抱歉要求这个琐碎的事情,但我似乎无法正确解决,因此我想我完全误解了我以为我知道的有关内存管理的所有知识。

I have a function that parses a network reply for some data and it looks like this: 我有一个解析某些数据的网络回复的函数,它看起来像这样:

// Call from another function
    QVariantMap *mappedResult = handleReply(reply);
...
// Function
QVariantMap *handleReply(QNetworkReply *reply) {
    QVariantMap *result = new QVariantMap;
    QVariant testvalue = new QVariant("testvalue");
    result->insert("testkey", testvalue);
    if (reply->error() > 0) {
        qDebug() << "Error number = " + reply->errorString();
        QVariant variant = QVariant.fromValue(reply->errorString());
        result->insert("error", variant);
    } else {
        QJsonDocument jsonResponse = QJsonDocument::fromJson(jsonString.toUtf8());
        QJsonObject jsonResponseObject = jsonResponse.object();
        *result = jsonResponseObject.toVariantMap();
    }
    return result;
}

If there is no error, the result is parsed fine by the built in toVariantMap function. 如果没有错误,则内置的toVariantMap函数可以很好地解析结果。 When there is an error however, I would like to create an entry in the result that is sent back from the function. 但是,如果出现错误,我想在从函数发送回的结果中创建一个条目。 As you can see in the function, I'm trying to create QVariants from Strings in two different ways. 正如您在函数中看到的那样,我试图以两种不同的方式从Strings创建QVariant。

The first approach gives an error like this: 第一种方法给出这样的错误:

C:\Qt\5.2.0\mingw48_32\include\QtCore\qvariant.h:466: error: 'QVariant::QVariant(void*)' is private inline QVariant(void *) Q_DECL_EQ_DELETE;
        ^

The second approach like this: 第二种方法是这样的:

[PATH] error: expected primary-expression before '.' token QVariant variant = QVariant.fromValue(reply->errorString());

I've also tried setting the values like this: 我也尝试过像这样设置值:

result->insert("testkey", "testvalue");
result->insert("error", reply->errorString());

The last approach doesn't give compile errors but the result variable in the return statement cannot be inspected in the debugger and as soon as the return statement is executed the application crashes with memory problems indicating I'm not using "new" properly. 最后一种方法不会产生编译错误,但是在调试器中无法检查return语句中的结果变量,并且一旦执行return语句,应用程序便因内存问题而崩溃,表明我没有正确使用“ new”。

"Error accessing memory address 0x...".

If someone with at least a little knowledge could help me sort out how to perform this simple task, I would be very greatful. 如果至少有一点知识的人可以帮助我理清如何执行此简单任务,我将非常感激。 How can I return a QVariantMap from my function with custom strings as key/value pairs? 如何使用自定义字符串作为键/值对从函数返回QVariantMap?

Cheers. 干杯。

I would write your function in the following way (with fixes): 我将以以下方式(带有修复程序)编写您的函数:

QVariantMap *handleReply(QNetworkReply *reply) {
    QVariantMap *result = new QVariantMap;
    QVariant testvalue("testvalue"); // <- fixed here
    result->insert("testkey", testvalue);
    if (reply->error() > 0) {
        qDebug() << "Error number = " + reply->errorString();
        QVariant variant(reply->errorString()); // <- fixed here
        result->insert("error", variant);
    } else {
        QJsonDocument jsonResponse = QJsonDocument::fromJson(jsonString.toUtf8());
        QJsonObject jsonResponseObject = jsonResponse.object();
        *result = jsonResponseObject.toVariantMap();
    }
    return result;
}

However it is still unclear, why do you need to work with a pointer to the variant map instread of passing it by value? 但是仍然不清楚,为什么您需要使用指向通过值传递变量映射的变体映射的指针?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM