Trying to implement input filtering. I want to make the user enter in the form of a string some logical expression of the type (x > 100 && x < 120) || x ==42 (x > 100 && x < 120) || x ==42 . In the code, x will be replaced with a specific constant. Actually the question is how to calculate it? In principle, QScriptEngine::evaluate() does an excellent job with this task, but to pull a whole Qt Script into a project for the sake of one line of code is very cool, in my opinion.

In short, is it possible to somehow do without Qt Script, and not to write your own parser from scratch?

  • 1) You can give a string to an external utility, for example, by substituting variables first. 2) An example of a calculator for Qt is in the documentation. - PinkTux
  • @PinkTux The version with an external utility is even worse - you need to drag the utility behind you. In systems with a package manager there are no problems (although there are no problems with Qt), but in the window pane ... - gbg
  • You yourself can implement the prasing of this line through the state machine. Maybe you can find on the Internet ready implementations of such functionality. - Unick
  • one
    What about QExpressionEvaluator ? - AivanF.
  • one
    BTW, crazy idea :) We take PicoC or TCC, we create something like char s[] = "int foo(int x) { return (x > 100 && x < 120) || x == 42; }" . We transfer this case as a string in the interpreter API and get the result :) The technology is slightly different in both cases (the API is slightly different), but the principle and the result are the same. But the possibilities are much greater. - PinkTux

1 answer 1

The muParser library works great with Qt, even with variables and functions.