Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved]If 8+0-5*7/3 is the text entered in the Line Edit widget ,how can we convert it to integer data type ?



  • If 8+0-5*7/3 is the text entered in the Line Edit widget ,how can we convert it to integer data type ?


  • Moderators

    No easy way in C++. You can try (I've never done that, but it might work) evaluating the expression using QScript, or writing your own parser for the input data.



  • As sierdzio has said, you can see "here":http://qt-project.org/doc/qt-4.8/qscriptengine.html :

    bq. Evaluating Scripts
    Use evaluate() to evaluate script code; this is the C++ equivalent of the built-in script function eval().

    @QScriptEngine myEngine;
    QScriptValue three = myEngine.evaluate("1 + 2");@

    bq. evaluate() returns a QScriptValue that holds the result of the evaluation. The QScriptValue class provides functions for converting the result to various C++ types (e.g. QScriptValue::toString() and QScriptValue::toNumber()).

    So, you can do what you want without a parser.



  • I use python's eval() function solved it ! thanks all your replies !


Log in to reply