Evaluate expression inside QML from C++



  • Hi everyone,

    I have a simple formula that changes all the time, so I was wondering if it would be possible to have that formula inside a qml file, which then is able to get the values form the user in my main c++ app, and return a value.
    That way I should be able to alter the formula in the qml file without having to recompile everything.

    Is that possible, and if indeed it is, where could I find a tutorial that will help me go on about this.

    Thanks for your time.



  • Hi ealione.

    Would it not be possible to create a config file that your application loads and reads at run-time. That way you can change your formula in the config without having to recompile your application.

    Also, this thread might have been more relevant in the Qt Quick forum.



  • Hi moeg,

    I decided to write this here because my app is in c++ and I was just thinking about using qml, if this is not the appropriate place for this post though the moderators can freely move it at any time.

    Regarding your suggestion, I would rather not go that way as maybe in the future the need arises for me to extended these limited "scripting" capabilities of my app. I was actually searching on QtScript, but I am yet to find a way to exchange data between my script and the app.


  • Moderators



  • Well then, I'm sure we can help you figure out a way to get the desired behavior you require from qml.

    It is definitely possible for qml to communicate with c++ and vice versa.

    Have a look at this for a start: "Integrating QML and C++":http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html



  • Thanks for the links, I will study them and report back, hopefully with a working example.



  • I did some tests and as expected I run into trouble.

    I made a basic form with random buttons, labels and text edits. I then added in the forms' menu an option called "script" that opens a second form that contains just a text edit for entering data and an "evaluate expression" button.

    What I wanted to do is be able to retrieve or set data from the main forms' various text edits labels etc, but I don't know how to configure the scriptEngine from my secondary form. I have something like

    @scriptingconsole::scriptingconsole(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::scriptingconsole)
    {
    ui->setupUi(this);

    QScriptValue appContext = MyEngine.newQObject(parent);
    MyEngine.globalObject().setProperty("app", appContext);    
    

    } @

    but it does not work, as if I try to evaluate the expression "app;" it will return null, and I don't have access to any other objects or public properties of my main form.

    Any idea what I am doing wrong here?

    NOTE: Since as I mentioned above my main interest is making my app scriptable, for the moment I moved to QtScript, and not QML. Should I change the title to the post or make a new one with altogether?

    Thank you for your time.



  • Hey,

    i assume your formula is a javascript function inside QML. In that case you could dynamically create a QML Component(with the javascript-function) in C++ and pass it to QML. From there you can access the function inside your QML Component and from C++ you can access your dynamically created Object.

    If you want to change the formula: change the plaintext for your QML Component and therefore the formula and pass it to QML again, overwriting your old QML Component/formula.



  • For the moment what you are saying is over my head. I am actually doing some test without qml just using a scriptEngine.

    Plus I just use the aforementioned "scripting console" (a simple class with a GUI) to enter a js command.

    So inside the scripting console implementation I set up a script engine (as shown in my original post) I read the text and pass it to that engine for evaluation.

    This works but only if I try to pass "this" (referring to the current script console) as an object to my engine.

    What I truly want to do though is somehow pass my main window where all the app functions are.

    I hope this makes a little bit clearer what I want to achieve.



  • Sorry, my bad, i was only referring to your first post because i thought the others were just some experiments... i'll see what i can find out.



  • Basically at this point this is all that I am doing, random experiments. It just seemed more reasonable to use QtScipt, which worked fine as long as I had it implemented inside my main class, but once I tried separating it in its own class with a separate UI I could not anymore access the functions located in the main window. In any case thanks for the help.



  • The answer for this last thing was in the way I instantiated my script console class as it was pointed out to me "here":http://stackoverflow.com/questions/27902070/using-qtscript-outside-my-main-form/27918306#27918306


Log in to reply
 

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