Yes, this approach is good. I've used it for a similar task. Note that QScriptEngine
uses JavaScript syntax, not C++ syntax. But JavaScript syntax is powerful and fulfills usual needs of user-defined formulas. It supports regular operators, math functions, brackets, local variables, etc.
You can store a formula in QString
. If you need to execute the same formula multiple times, you should use QScriptProgram
to compile a formula before executing.