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.2k 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on 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

    mrjjM 1 Reply Last reply
    0
    • SPlattenS SPlatten

      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.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #5

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

      1 Reply Last reply
      2
      • SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on 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

        mrjjM 1 Reply Last reply
        0
        • SPlattenS SPlatten

          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' ?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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
          • SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on 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 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
              • SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #10

                Thank you, I will take a look now.

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on 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 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
                    • SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on 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