Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt C++ application connecting signals to JavaScript?
Forum Updated to NodeBB v4.3 + New Features

Qt C++ application connecting signals to JavaScript?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 3.1k Views 3 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 5 Dec 2018, 19:50 last edited by SPlatten 12 Jun 2018, 07:39
    #1

    I am developing a Qt application that implements a GUI based on an XML file, so far so good, now I want to add Javascript to the XML file and connect signals from Qt controls to Javascript functions.

    I've seen this on QML and Javascript:
    http://doc.qt.io/archives/qt-4.8/qdeclarativejavascript.html

    Is it possible from a Qt C++ application?

    Here is the definition that creates a QPushButton:

    <button id="btn1" x="160" y="116" width="80"
            properties="QPushButton {border: 1px solid yellow;
    		          border-radius: 4px;
    		   		padding: 1px 18px 1px 3px;
    			      min-width: 6em}">						
        <state value="off" text="Off"/>
        <state value="on" text="On"/>
        <signal name="pbtnClicked">
    	<subscriber sid="win2:title"/>
    	<script>
    	    function onClick() {
                };					
    	</script>
        </signal>	
        <signal name="pbtnPressed"/>
        <signal name="pbtnReleased"/>
        <signal name="pbtnToggled"/>
    </button>
    

    What I would like to do is be able to connect the javascript function with the signal in C++...

    Can this be done?

    Kind Regards,
    Sy

    R 1 Reply Last reply 5 Dec 2018, 21:06
    0
    • S SPlatten
      5 Dec 2018, 19:50

      I am developing a Qt application that implements a GUI based on an XML file, so far so good, now I want to add Javascript to the XML file and connect signals from Qt controls to Javascript functions.

      I've seen this on QML and Javascript:
      http://doc.qt.io/archives/qt-4.8/qdeclarativejavascript.html

      Is it possible from a Qt C++ application?

      Here is the definition that creates a QPushButton:

      <button id="btn1" x="160" y="116" width="80"
              properties="QPushButton {border: 1px solid yellow;
      		          border-radius: 4px;
      		   		padding: 1px 18px 1px 3px;
      			      min-width: 6em}">						
          <state value="off" text="Off"/>
          <state value="on" text="On"/>
          <signal name="pbtnClicked">
      	<subscriber sid="win2:title"/>
      	<script>
      	    function onClick() {
                  };					
      	</script>
          </signal>	
          <signal name="pbtnPressed"/>
          <signal name="pbtnReleased"/>
          <signal name="pbtnToggled"/>
      </button>
      

      What I would like to do is be able to connect the javascript function with the signal in C++...

      Can this be done?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 5 Dec 2018, 21:06 last edited by raven-worx 12 May 2018, 21:08
      #2

      @SPlatten said in Qt C++ application connecting signals to JavaScript?:

      What I would like to do is be able to connect the javascript function with the signal in C++

      Something like this?

      QJSEngine* engine = ...;
      connect( button, &QPushButton::clicked, this, [engine] () {
           QString script = ...
           QJSValue returnValue = engine->evaluate(script);
      }
      

      See the docs for how the script should look like

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SPlatten
        wrote on 5 Dec 2018, 21:07 last edited by
        #3

        Thank you, I'll take a look.

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SPlatten
          wrote on 23 Dec 2018, 18:16 last edited by SPlatten
          #4

          What sort of thing can QJSEngine do and not do?

          In my simple test code I tried:

              QObject::connect(pobjPushButton
                              ,&QPushButton::clicked
                              ,[pobjScriptEng]() {
                                   pobjScriptEng->evaluate("alert('hello');");
                                                 });
          

          I put a break point on the line inside the callback and when I clicked on the pushbutton I could see the debugger stop on the evaluate line, but having executed the line, I didn't the alert displayed and no error was thrown.

          Can anyone help?

          I've got a little bit further by including a call:

              mspobjJSeng->installExtensions(QJSEngine::AllExtensions);
          

          I don't think 'alert()' is supported.

          Kind Regards,
          Sy

          M 1 Reply Last reply 23 Dec 2018, 19:22
          0
          • S SPlatten
            23 Dec 2018, 18:16

            What sort of thing can QJSEngine do and not do?

            In my simple test code I tried:

                QObject::connect(pobjPushButton
                                ,&QPushButton::clicked
                                ,[pobjScriptEng]() {
                                     pobjScriptEng->evaluate("alert('hello');");
                                                   });
            

            I put a break point on the line inside the callback and when I clicked on the pushbutton I could see the debugger stop on the evaluate line, but having executed the line, I didn't the alert displayed and no error was thrown.

            Can anyone help?

            I've got a little bit further by including a call:

                mspobjJSeng->installExtensions(QJSEngine::AllExtensions);
            

            I don't think 'alert()' is supported.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 23 Dec 2018, 19:22 last edited by
            #5

            Hi
            http://doc.qt.io/qt-5/qjsengine.html#details
            Try
            console.info()
            instead.

            1 Reply Last reply
            2
            • S Offline
              S Offline
              SPlatten
              wrote on 23 Dec 2018, 19:25 last edited by
              #6

              Thank you, info does work and has the advantage of adding a new line for each entry...

              I've just put in:

                  console.info("QJSEngine");
                  console.info(this);
              

              Is there anyway to debug the script or to see what members are visible in 'this' ?

              Kind Regards,
              Sy

              M 1 Reply Last reply 23 Dec 2018, 19:33
              0
              • S SPlatten
                23 Dec 2018, 19:25

                Thank you, info does work and has the advantage of adding a new line for each entry...

                I've just put in:

                    console.info("QJSEngine");
                    console.info(this);
                

                Is there anyway to debug the script or to see what members are visible in 'this' ?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 23 Dec 2018, 19:33 last edited by
                #7

                Hi
                As far as i know, you cant.
                However, if you see in
                https://doc.qt.io/qt-5.11/whatsnew57.html
                section Qt QML Module
                says
                "Enabled all debug services to work with QJSEngine (instead of QQmlEngine), which allows non-QML JavaScript debugging or profiling."

                so i assume if you have a js file, you can actualy debug this and see members.
                However, Not inside a mere string in a c++ file.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SPlatten
                  wrote on 23 Dec 2018, 19:37 last edited by
                  #8

                  I've hit another problem now, I'm trying to pass in a QString containing a script that was read from file, however when I try to assign this:

                      QObject::connect(pobjPushButton
                                      ,&QPushButton::clicked
                                      ,[pobjScriptEng]() {
                                  pobjScriptEng->evaluate(static_cast<const QString>(strScript));
                                              });
                  

                  I get:

                     variable 'strScript' cannot be implicitly captured in a lambda with no capture-default specified
                  

                  What does this mean?

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  0
                  • Gojir4G Offline
                    Gojir4G Offline
                    Gojir4
                    wrote on 23 Dec 2018, 19:50 last edited by
                    #9

                    Hi @SPlatten,

                    This means you need to "capture" the variable you want to use in the lambda. This tells to the compiler that have to keep the variable, usually a local one, in the memory to be accessible when the lambda is called.

                    Capturing the variable is done by adding its name inside the brackets []. You can also add & suffix for passing variable by reference, in.

                        QObject::connect(pobjPushButton
                                        ,&QPushButton::clicked
                                        ,[pobjScriptEng, &strScript]() { //or [&] to use the "by-reference capture default" mode
                                    pobjScriptEng->evaluate(static_cast<const QString>(strScript));
                                                });
                    

                    More informations here: https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture

                    1 Reply Last reply
                    3
                    • S Offline
                      S Offline
                      SPlatten
                      wrote on 24 Dec 2018, 10:00 last edited by
                      #10

                      Thank you, I will take a look now.

                      Kind Regards,
                      Sy

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SPlatten
                        wrote on 24 Dec 2018, 11:05 last edited by
                        #11

                        Is there any way to link the signal with a specific function in the script?

                        Kind Regards,
                        Sy

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on 24 Dec 2018, 11:21 last edited by
                          #12

                          It's all in the docs http://doc.qt.io/qt-5/qjsengine.html

                          QJSvalue module = myEngine.importModule("./math.mjs");
                          QJSValue sumFunction = module.property("sum");
                          QJSValue result = sumFunction.call(args);
                          

                          This will call the function sum from the module math.mjs passing args as arguments

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          5
                          • S Offline
                            S Offline
                            SPlatten
                            wrote on 25 Dec 2018, 10:27 last edited by
                            #13

                            Thank you, I will have a good read of the documentation....

                            Kind Regards,
                            Sy

                            1 Reply Last reply
                            0

                            • Login

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