executing contents of QString?



  • Hi guys.

    I have a little problem here I am trying to work. If I have for example the following string:

    QString rule = "if (a=1 && b=1) {c=1}";

    Is there any way I can get my application to actually execute this if statement inside a loop?
    I have done this before using linux bash and PHP but not sure how it's done using QT.

    Thanks guys


  • Qt Champions 2016

    Hi and welcome
    You can have a look at
    http://doc.qt.io/qt-5/qtscript-index.html

    It would also be possible to run a bash script from Qt program and grab any output so it
    depends on what you need.



  • @mrjj

    Hi thanks for the link. I shall read it in full now but I'm a bit anxious as it does look complicated.
    An external bash script would not work as I need QT to execute this code using it's local variables. These variables are read from a database and then concatenated into that rule string as shown above. I simply want to get it to execute the contents.

    Anyway thanks again. I will look into this scripting stuff


  • Moderators

    C++ is not a scripting language. It is compiled into a binary and you can't execute arbitrary code at runtime like in bash or php.
    To have it evaluated at runtime you would have to bulk a c++ compiler with your app, put the string into some executable unit like a function, compile it, link into a library, load and execute.

    The only other solution is scripting i.e. using another language that has an interpreter evaluating expressions. One such language is JavaScript that Qt has some support for. Be aware that QtScript, as suggested by @mrjj, is not really maintained anymore and is going to be removed from Qt soon. The replacement for it is possibly going to be QJSEngine, although it's still under development and there's no feature parity yet.



  • @Chris-Kawa

    Ok understood. I appreciate your time. I will just not bother with this particular functionality


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.