fralx / LimeReport

Report generator for Qt Framework
http://limereport.ru/
Other
403 stars 154 forks source link

How about is the plan of supporting QJson type variable in LimeReport? #359

Closed liufeijin closed 2 years ago

liufeijin commented 3 years ago

Dear Fralx Do you have plan for adding QJson type variable in LimeReport? When using Limereport, need pass some datas to Limereprot variable from my QT app, if it is QJson type will need a lot of code to split QJson to QString and pass to Limereport. If Limereport can support QJson variables , just pass a few data to Limereprot and there is only a few variables in Limereport also. It will be concise.

fralx commented 2 years ago

Hi! You can do this trick:

QJSValue Convert(QJSEngine* engine, const QJsonValue& val)
{
    if (val.isBool())
    {
        return QJSValue(val.toBool());
    }
    else if (val.isString())
    {
        return QJSValue(val.toString());
    }
    else if (val.isDouble())
    {
        return QJSValue(val.toDouble());
    }
    else if (val.isNull())
    {
        return QJSValue(QJSValue::NullValue);
    }
    else if (val.isUndefined())
    {
        return QJSValue(QJSValue::UndefinedValue);
    }
    else if (val.isObject())
    {
        QJsonObject obj = val.toObject();
        QJSValue newobj = engine->newObject();
        for (auto itor = obj.begin(); itor != obj.end(); itor++)
        {
            QString key = itor.key();
            QJsonValue value = itor.value();
            QJSValue convertedValue = Convert(engine, value);
            newobj.setProperty(key, convertedValue);
        }
        return newobj;
    }
    else if (val.isArray())
    {
        QJsonArray arr = val.toArray();
        QJSValue newobj = engine->newArray(arr.size());
        for (int i = 0; i < arr.size(); i++)
        {
            QJsonValue value = arr[i];
            QJSValue convertedValue = Convert(engine, value);
            newobj.setProperty(i, convertedValue);
        }
        return newobj;
    }

    // ASSERT(FALSE && "This shouldn't happen");
    return QJSValue(QJSValue::UndefinedValue);

}
// https://stackoverflow.com/questions/49658551/convert-qjsonobject-to-javascript-object
.......
QJsonObject jsonItemObj;
jsonItemObj["Street"] = "Downing Street 10";
jsonItemObj["City"] = "London";
jsonItemObj["Country"] = "Great Britain";
report->scriptManager()->scriptEngine()->globalObject().setProperty(
    "test", Convert(report->scriptManager()->scriptEngine(),jsonItemObj)
);

in the report: $S{test["Street"]}

liufeijin commented 2 years ago

Hi fralx Many thanks!

fralx commented 2 years ago

Hi! You can also use a different approach :)

report->dataManager()->setReportVariable("Json", jDoc.toJson()); .... $S{ let j = JSON.parse(getVariable("Json")) j["City"] }

liufeijin commented 2 years ago

Hi! You can also use a different approach :)

report->dataManager()->setReportVariable("Json", jDoc.toJson()); .... $S{ let j = JSON.parse(getVariable("Json")) j["City"] }

This is better.