Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Evaluate expression inside QML from C++
Forum Updated to NodeBB v4.3 + New Features

Evaluate expression inside QML from C++

Scheduled Pinned Locked Moved QML and Qt Quick
12 Posts 4 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ealione
    wrote on 9 Jan 2015, 10:51 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      melghawi
      wrote on 9 Jan 2015, 11:11 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ealione
        wrote on 9 Jan 2015, 11:28 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 9 Jan 2015, 11:32 last edited by
          #4

          Hi,

          "This":http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html might help you.

          157

          1 Reply Last reply
          0
          • M Offline
            M Offline
            melghawi
            wrote on 9 Jan 2015, 11:37 last edited by
            #5

            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

            1 Reply Last reply
            0
            • E Offline
              E Offline
              ealione
              wrote on 9 Jan 2015, 12:16 last edited by
              #6

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

              1 Reply Last reply
              0
              • E Offline
                E Offline
                ealione
                wrote on 12 Jan 2015, 10:16 last edited by
                #7

                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.

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  onek24
                  wrote on 12 Jan 2015, 14:05 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    ealione
                    wrote on 12 Jan 2015, 14:15 last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      onek24
                      wrote on 12 Jan 2015, 14:18 last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • E Offline
                        E Offline
                        ealione
                        wrote on 12 Jan 2015, 14:23 last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          ealione
                          wrote on 13 Jan 2015, 09:19 last edited by
                          #12

                          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

                          1 Reply Last reply
                          0

                          1/12

                          9 Jan 2015, 10:51

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved