Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Formula in JSON
Hey i have a JSON like:
. . . "math": "(2.0 * value_x) + 5 * 25.0" . .
Now i want to define the value in my JSON ("value_x") in my Qt-Programm.
in Qt try this:
double value_x = 12.4; double function = value.at(0)->getValue().at(0)->math() // HERE I READ MY JSON AND I GET A STRING LIKE: "(2.0 * value_x) + 5 * 25.0" double result = HERE I WANT TO GET 2*12.4 +5 * 25.0
Any Idea how i can define a value in JSON and get the result from the formula (also from JSON) ?
@NotYourFan You can replace value_x with the number in the JSON string before evaluating it.
Isn't that the same question as your other thread here ?
i should define the "value_x" in Qt.
"value_x" is an Input in my GUI ... so i dont know what is value_x before i defined in my GUI ....
The next Point is that the "unknow value" (in this case value_x) has every time an other name.
also very similar to his other 2 topics
like I said before your best bet
is a Scriptinglanguage binding
@NotYourFan That's why I suggested to replace value_x with the number, didn't I?
"has every time an other name" - then how do you know what you have to define?
i solve my problem like this:
#include <QScriptEngine> . . QScriptEngine engine; engine.globalObject().setProperty(value.at(0)->getValue.at(0).name(), value.at(0)->getValue.at(0).math()); qDebug() << "foo times two is:" << engine.evaluate("foo * 2").toNumber();
THANKS specially @J-Hilk for the bullet point "script" :)
VRonin last edited by
You are using a bazooka to kill a fly: https://en.wikipedia.org/wiki/Shunting-yard_algorithm
Thank you for your Feedback.
I do this way because my JSON-File is every time diffrent.
So with this solution my formula works fine.