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. Help with timer and JavaScript slot.
Forum Updated to NodeBB v4.3 + New Features

Help with timer and JavaScript slot.

Scheduled Pinned Locked Moved Solved General and Desktop
43 Posts 4 Posters 5.4k Views 2 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.
  • K kshegunov
    30 Dec 2021, 06:52

    It's a runtime error, so I can't tell if this is the correct piece of code you're showing us. So:

    If the QT_FATAL_WARNINGS environment variable is set, qWarning() exits after printing the warning message. This makes it easy to obtain a backtrace in the debugger.

    From https://doc.qt.io/qt-5/debug.html.

    Set the environment variable and extract a stack trace; paste the trace here and we can talk more.

    S Offline
    S Offline
    SPlatten
    wrote on 30 Dec 2021, 06:53 last edited by SPlatten
    #3

    @kshegunov , I will look at the link and get back to you.

    I added:

    export QT_DEBUG_PLUGINS=1
    export QT_FATAL_WARNINGS=1
    

    To ~/.profile, I also went to Project Settings -> Environment and added the same environment variables to Qt Creator.

    When I build my project now it terminates instantly, this is the application output:

    2021-12-30 07:06:58.865187+0000 XMLMPAM[5523:56476] QFactoryLoader::QFactoryLoader() checking directory path "/Users/sy/Qt/5.15.2/clang_64/plugins/platforms" ...
    2021-12-30 07:06:58.874330+0000 XMLMPAM[5523:56476] QFactoryLoader::QFactoryLoader() looking at "/Users/sy/Qt/5.15.2/clang_64/plugins/platforms/libqcocoa.dylib"
    2021-12-30 07:06:58.874511+0000 XMLMPAM[5523:56476] Found metadata in lib /Users/sy/Qt/5.15.2/clang_64/plugins/platforms/libqcocoa.dylib, metadata=
    {
        "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
        "MetaData": {
            "Keys": [
                "cocoa"
            ]
        },
        "archreq": 0,
        "className": "QCocoaIntegrationPlugin",
        "debug": false,
        "version": 331520
    }
    

    This looks like a different issue...I've reverted from implementing these environment variables as this isn't the same issue and I cannot use these to get where I need to be to fault find.

    It seems the issue I'm having is using a lambda JavaScript function with the timer signal, can anyone help?

    Kind Regards,
    Sy

    J K 2 Replies Last reply 30 Dec 2021, 08:05
    0
    • S SPlatten
      30 Dec 2021, 06:53

      @kshegunov , I will look at the link and get back to you.

      I added:

      export QT_DEBUG_PLUGINS=1
      export QT_FATAL_WARNINGS=1
      

      To ~/.profile, I also went to Project Settings -> Environment and added the same environment variables to Qt Creator.

      When I build my project now it terminates instantly, this is the application output:

      2021-12-30 07:06:58.865187+0000 XMLMPAM[5523:56476] QFactoryLoader::QFactoryLoader() checking directory path "/Users/sy/Qt/5.15.2/clang_64/plugins/platforms" ...
      2021-12-30 07:06:58.874330+0000 XMLMPAM[5523:56476] QFactoryLoader::QFactoryLoader() looking at "/Users/sy/Qt/5.15.2/clang_64/plugins/platforms/libqcocoa.dylib"
      2021-12-30 07:06:58.874511+0000 XMLMPAM[5523:56476] Found metadata in lib /Users/sy/Qt/5.15.2/clang_64/plugins/platforms/libqcocoa.dylib, metadata=
      {
          "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
          "MetaData": {
              "Keys": [
                  "cocoa"
              ]
          },
          "archreq": 0,
          "className": "QCocoaIntegrationPlugin",
          "debug": false,
          "version": 331520
      }
      

      This looks like a different issue...I've reverted from implementing these environment variables as this isn't the same issue and I cannot use these to get where I need to be to fault find.

      It seems the issue I'm having is using a lambda JavaScript function with the timer signal, can anyone help?

      J Offline
      J Offline
      JonB
      wrote on 30 Dec 2021, 08:05 last edited by JonB
      #4

      @SPlatten said in Help with timer and JavaScript slot.:

      It seems the issue I'm having is using a lambda JavaScript function with the timer signal, can anyone help?

      I'm afraid I do not have the solution, but have you at least verified this claim? You may well be right, but why don't you temporarily test with a regular function slot so you know whether lambda is relevant to the issue? And a lambda slot on a non-timer signal.

      S 1 Reply Last reply 30 Dec 2021, 08:07
      0
      • J JonB
        30 Dec 2021, 08:05

        @SPlatten said in Help with timer and JavaScript slot.:

        It seems the issue I'm having is using a lambda JavaScript function with the timer signal, can anyone help?

        I'm afraid I do not have the solution, but have you at least verified this claim? You may well be right, but why don't you temporarily test with a regular function slot so you know whether lambda is relevant to the issue? And a lambda slot on a non-timer signal.

        S Offline
        S Offline
        SPlatten
        wrote on 30 Dec 2021, 08:07 last edited by
        #5

        @JonB , I know the timer and connection to a slot works perfectly fine as I use this technique elsewhere.

        Kind Regards,
        Sy

        J 1 Reply Last reply 30 Dec 2021, 08:11
        0
        • S SPlatten
          30 Dec 2021, 06:53

          @kshegunov , I will look at the link and get back to you.

          I added:

          export QT_DEBUG_PLUGINS=1
          export QT_FATAL_WARNINGS=1
          

          To ~/.profile, I also went to Project Settings -> Environment and added the same environment variables to Qt Creator.

          When I build my project now it terminates instantly, this is the application output:

          2021-12-30 07:06:58.865187+0000 XMLMPAM[5523:56476] QFactoryLoader::QFactoryLoader() checking directory path "/Users/sy/Qt/5.15.2/clang_64/plugins/platforms" ...
          2021-12-30 07:06:58.874330+0000 XMLMPAM[5523:56476] QFactoryLoader::QFactoryLoader() looking at "/Users/sy/Qt/5.15.2/clang_64/plugins/platforms/libqcocoa.dylib"
          2021-12-30 07:06:58.874511+0000 XMLMPAM[5523:56476] Found metadata in lib /Users/sy/Qt/5.15.2/clang_64/plugins/platforms/libqcocoa.dylib, metadata=
          {
              "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
              "MetaData": {
                  "Keys": [
                      "cocoa"
                  ]
              },
              "archreq": 0,
              "className": "QCocoaIntegrationPlugin",
              "debug": false,
              "version": 331520
          }
          

          This looks like a different issue...I've reverted from implementing these environment variables as this isn't the same issue and I cannot use these to get where I need to be to fault find.

          It seems the issue I'm having is using a lambda JavaScript function with the timer signal, can anyone help?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 30 Dec 2021, 08:08 last edited by
          #6

          @SPlatten said in Help with timer and JavaScript slot.:

          export QT_DEBUG_PLUGINS=1

          I didn't say you should include this. This is when you're attempting to track errors in the plugins loading, you're not, are you?

          Read and abide by the Qt Code of Conduct

          S 1 Reply Last reply 30 Dec 2021, 08:11
          1
          • S SPlatten
            30 Dec 2021, 08:07

            @JonB , I know the timer and connection to a slot works perfectly fine as I use this technique elsewhere.

            J Offline
            J Offline
            JonB
            wrote on 30 Dec 2021, 08:11 last edited by
            #7

            @SPlatten
            Based on nothing, I'd find it "surprising" if the existence of a lambda in itself causes a thread to be spawned/involved.

            Meantime I think you should follow @kshegunov's request: test QT_FATAL_WARNINGS=1 but not with QT_DEBUG_PLUGINS=1.

            S 1 Reply Last reply 30 Dec 2021, 08:12
            0
            • K kshegunov
              30 Dec 2021, 08:08

              @SPlatten said in Help with timer and JavaScript slot.:

              export QT_DEBUG_PLUGINS=1

              I didn't say you should include this. This is when you're attempting to track errors in the plugins loading, you're not, are you?

              S Offline
              S Offline
              SPlatten
              wrote on 30 Dec 2021, 08:11 last edited by
              #8

              @kshegunov , with just:

              QT_FATAL_WARNINGS
              

              set to 1, I don't get the initial problem however the result is no different and in my debug output I still get:

              S000000000029E000000002424T08:09:54.416W:QObject: Cannot create children for a parent that is in a different thread.
              (Parent is QNativeSocketEngine(0x6000000122f0), parent's thread is clsServer(0x600000252c00), current thread is clsThread(0x600002632700)
              S000000000030E000000002424T08:09:54.416W:QObject: Cannot create children for a parent that is in a different thread.
              (Parent is QNativeSocketEngine(0x600000014140), parent's thread is clsServer(0x600000252140), current thread is clsThread(0x600002632700)
              S000000000031E000000002424T08:09:54.416W:QObject: Cannot create children for a parent that is in a different thread.
              (Parent is QNativeSocketEngine(0x60000000b0b0), parent's thread is clsServer(0x600000250b60), current thread is clsThread(0x600002632700)
              

              Where everything before : is my own tracking information.

              Kind Regards,
              Sy

              K 1 Reply Last reply 30 Dec 2021, 08:16
              0
              • J JonB
                30 Dec 2021, 08:11

                @SPlatten
                Based on nothing, I'd find it "surprising" if the existence of a lambda in itself causes a thread to be spawned/involved.

                Meantime I think you should follow @kshegunov's request: test QT_FATAL_WARNINGS=1 but not with QT_DEBUG_PLUGINS=1.

                S Offline
                S Offline
                SPlatten
                wrote on 30 Dec 2021, 08:12 last edited by SPlatten
                #9

                @JonB I think the thread is related to the JavaScript function call in the Lambda:

                QMetaObject::Connection cn = QObject::connect(ptmrScript
                                                             ,&QTimer::timeout
                                                             ,[pobjScriptEng
                                                             ,ptmrScript
                                                             ,strFile
                                                             ,strFunc
                                                             ,strScriptWithCall]() {
                   QJSValue objResult(pobjScriptEng->evaluate(strScriptWithCall));
                   QString strError;
                   if ( objResult.isError() == true ) {
                       strError = QString("%1\n%2\n%3").arg(strFile).arg(strFunc)
                                                       .arg(objResult.toString());
                   } else {
                       QString strResult(objResult.toString());
                       if ( strResult.compare("undefined") != 0 ) {
                           strError = strResult;
                       }
                    }
                    if ( strError.isEmpty() != true ) {
                        clsXMLnode* pobjRoot(clsXMLnode::spobjGetRoot());
                        emit pobjRoot->error(strError);
                        ptmrScript->deleteLater();
                    }
                });
                

                Kind Regards,
                Sy

                J 1 Reply Last reply 30 Dec 2021, 08:15
                0
                • S SPlatten
                  30 Dec 2021, 08:12

                  @JonB I think the thread is related to the JavaScript function call in the Lambda:

                  QMetaObject::Connection cn = QObject::connect(ptmrScript
                                                               ,&QTimer::timeout
                                                               ,[pobjScriptEng
                                                               ,ptmrScript
                                                               ,strFile
                                                               ,strFunc
                                                               ,strScriptWithCall]() {
                     QJSValue objResult(pobjScriptEng->evaluate(strScriptWithCall));
                     QString strError;
                     if ( objResult.isError() == true ) {
                         strError = QString("%1\n%2\n%3").arg(strFile).arg(strFunc)
                                                         .arg(objResult.toString());
                     } else {
                         QString strResult(objResult.toString());
                         if ( strResult.compare("undefined") != 0 ) {
                             strError = strResult;
                         }
                      }
                      if ( strError.isEmpty() != true ) {
                          clsXMLnode* pobjRoot(clsXMLnode::spobjGetRoot());
                          emit pobjRoot->error(strError);
                          ptmrScript->deleteLater();
                      }
                  });
                  
                  J Offline
                  J Offline
                  JonB
                  wrote on 30 Dec 2021, 08:15 last edited by JonB
                  #10

                  @SPlatten
                  I just don't see where/why a lambda will behave differently from a C++ method here, as you say it does. Maybe your issue lies in the calls of the body of the lambda, which is a different matter, and ought show up equally in a method instead.

                  But ignore me because I won't have a solution. @kshegunov is much more likely to spot/offer one... :)

                  1 Reply Last reply
                  0
                  • S SPlatten
                    30 Dec 2021, 08:11

                    @kshegunov , with just:

                    QT_FATAL_WARNINGS
                    

                    set to 1, I don't get the initial problem however the result is no different and in my debug output I still get:

                    S000000000029E000000002424T08:09:54.416W:QObject: Cannot create children for a parent that is in a different thread.
                    (Parent is QNativeSocketEngine(0x6000000122f0), parent's thread is clsServer(0x600000252c00), current thread is clsThread(0x600002632700)
                    S000000000030E000000002424T08:09:54.416W:QObject: Cannot create children for a parent that is in a different thread.
                    (Parent is QNativeSocketEngine(0x600000014140), parent's thread is clsServer(0x600000252140), current thread is clsThread(0x600002632700)
                    S000000000031E000000002424T08:09:54.416W:QObject: Cannot create children for a parent that is in a different thread.
                    (Parent is QNativeSocketEngine(0x60000000b0b0), parent's thread is clsServer(0x600000250b60), current thread is clsThread(0x600002632700)
                    

                    Where everything before : is my own tracking information.

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 30 Dec 2021, 08:16 last edited by
                    #11

                    @SPlatten said in Help with timer and JavaScript slot.:

                    I don't get the initial problem however the result is no different and in my debug output I still get:

                    It is not a fix, it's a technique to track down where the problem occurs. Also, "debug output" != "stack trace". When the debugger trips the assertion with QT_FATAL_WARNINGS on it's going to stop in all threads. Then you can go to creator's call stack view (or whatever IDE you're using) and you can extract the exact backtrace, which is what I requested. I don't expect the output the change, as nothing's been done at this point beside making the program crash, instead of silently (and wrongfully) succeeding.

                    @JonB I think the thread is related to the JavaScript function call in the Lambda

                    Why? How do you figure this out? I have no indication that this is where the warning's been emitted, it can be anyplace else.

                    Read and abide by the Qt Code of Conduct

                    S 1 Reply Last reply 30 Dec 2021, 08:17
                    1
                    • K kshegunov
                      30 Dec 2021, 08:16

                      @SPlatten said in Help with timer and JavaScript slot.:

                      I don't get the initial problem however the result is no different and in my debug output I still get:

                      It is not a fix, it's a technique to track down where the problem occurs. Also, "debug output" != "stack trace". When the debugger trips the assertion with QT_FATAL_WARNINGS on it's going to stop in all threads. Then you can go to creator's call stack view (or whatever IDE you're using) and you can extract the exact backtrace, which is what I requested. I don't expect the output the change, as nothing's been done at this point beside making the program crash, instead of silently (and wrongfully) succeeding.

                      @JonB I think the thread is related to the JavaScript function call in the Lambda

                      Why? How do you figure this out? I have no indication that this is where the warning's been emitted, it can be anyplace else.

                      S Offline
                      S Offline
                      SPlatten
                      wrote on 30 Dec 2021, 08:17 last edited by
                      #12

                      @kshegunov , please see edit of my last post which includes the body of the lambda. The execution doesn't stop, there is just output to the debug.

                      Kind Regards,
                      Sy

                      K 1 Reply Last reply 30 Dec 2021, 08:20
                      0
                      • S SPlatten
                        30 Dec 2021, 08:17

                        @kshegunov , please see edit of my last post which includes the body of the lambda. The execution doesn't stop, there is just output to the debug.

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 30 Dec 2021, 08:20 last edited by kshegunov
                        #13

                        @SPlatten said in Help with timer and JavaScript slot.:

                        @kshegunov , please see edit of my last post which includes the body of the lambda. The execution doesn't stop, there is just output to the debug.

                        I have a fairly good idea where the problem may be, but I prefer not to speculate at this point. If the program doesn't stop and the mentioned environment variable is set, then you're not running your application in debug mode - check this and adjust accordingly.

                        Read and abide by the Qt Code of Conduct

                        S 1 Reply Last reply 30 Dec 2021, 08:25
                        0
                        • K kshegunov
                          30 Dec 2021, 08:20

                          @SPlatten said in Help with timer and JavaScript slot.:

                          @kshegunov , please see edit of my last post which includes the body of the lambda. The execution doesn't stop, there is just output to the debug.

                          I have a fairly good idea where the problem may be, but I prefer not to speculate at this point. If the program doesn't stop and the mentioned environment variable is set, then you're not running your application in debug mode - check this and adjust accordingly.

                          S Offline
                          S Offline
                          SPlatten
                          wrote on 30 Dec 2021, 08:25 last edited by SPlatten
                          #14

                          @kshegunov , 100% I'm in debug, I'm running in Qt Creator debug and developing. I have a function called createTimer here is the prototype:

                          Q_INVOKABLE void createTimer(const QString& crstrID
                                                      ,const uint cuintInterval
                                                      ,const QString& crstrFunction);
                          

                          The implementation:

                          /**
                           * @brief clsScriptHelper::createTimer
                           * @param crstrID : Constant reference to Timer ID
                           * @param cuintInterval : Constant millisecond interval
                           * @param crstrFunction : Constant reference to JSfile@Function
                           */
                          void clsScriptHelper::createTimer(const QString& crstrID, const uint cuintInterval
                                                           ,const QString& crstrFunction) {
                              if ( crstrID.trimmed().isEmpty() == true || cuintInterval == 0
                                || crstrFunction.trimmed().isEmpty() == true
                                || crstrFunction.indexOf(clsXMLnode::msccScriptDelimiter) == -1 ) {
                              //One or more of the parameters are invalid, do nothing
                                  return;
                              }
                              //Get Application instance
                              clsMainWnd* pAppInstance(clsMainWnd::pobjGetAppWnd());
                              if ( pAppInstance == nullptr ) {
                              //Cannot get application instance!
                                  return;
                              }
                              //Look for registered timer
                              QTimer* ptmrScript(pAppInstance->ptmrGetTimer(crstrID));
                              if ( ptmrScript != nullptr ) {
                              //Already registered, do nothing
                                  return;
                              }
                              //No timer locatd with that ID, its safe to proceed
                              QStringList slstScript = crstrFunction.split(clsXMLnode::msccScriptDelimiter);
                              mpScripts* pmpScriptsMap;
                              if ( slstScript.length() == SSF_PARAM_COUNT
                              && (pmpScriptsMap = clsMainWnd::pmpGetScriptsMap()) != nullptr ) {
                              //Look for the script in the scripts map
                                  mpScripts::iterator itrScript(pmpScriptsMap->find(slstScript[SFF_FILE_IDX]));
                                  if ( itrScript != pmpScriptsMap->end() ) {
                              //Found it, build the script up ready for the signal connection
                                      QString strScript(itrScript->second);
                              //Get pointer to script engine
                                      QJSEngine* pobjScriptEng(clsJSON::pobjGetScriptEng());
                              //Create new timer
                                      ptmrScript = new QTimer();
                              //Register the new timer using the supplied ID
                                      pAppInstance->registerTimer(ptmrScript, crstrID);
                              //Add any globals to the script
                                      clsScriptHelper::pobjGetInstance()->addGlobals(pobjScriptEng);
                              //Build up script function call
                                      QString strFile(slstScript[SFF_FILE_IDX])
                                             ,strCall(slstScript[SSF_FUNCTION_IDX])
                                             ,strScriptWithCall(strScript)
                                             ,strFunc(strCall
                                                    + QString(clsDebugService::msccBrktOpen)
                                                    + QString(clsDebugService::msccBrktClose)
                                                    + QString(clsXMLnode::msccScriptFunctionTerminator));
                                       if ( strScriptWithCall.endsWith(clsXMLnode::msccScriptFunctionTerminator) != true ) {
                                           strScriptWithCall += clsXMLnode::msccScriptFunctionTerminator;
                                       }
                                       strScriptWithCall += strFunc;
                              //Create connection to timer and script
                                      QMetaObject::Connection cn = QObject::connect(ptmrScript
                                                                                   ,&QTimer::timeout
                                                                                   ,[pobjScriptEng
                                                                                    ,ptmrScript
                                                                                    ,strFile
                                                                                    ,strFunc
                                                                                    ,strScriptWithCall]() {
                                           QJSValue objResult(pobjScriptEng->evaluate(strScriptWithCall));
                                           QString strError;
                                           if ( objResult.isError() == true ) {
                                               strError = QString("%1\n%2\n%3").arg(strFile).arg(strFunc)
                                                                               .arg(objResult.toString());
                                           } else {
                                               QString strResult(objResult.toString());
                                               if ( strResult.compare("undefined") != 0 ) {
                                                   strError = strResult;
                                               }
                                           }
                                           if ( strError.isEmpty() != true ) {
                                               clsXMLnode* pobjRoot(clsXMLnode::spobjGetRoot());
                                               emit pobjRoot->error(strError);
                                               ptmrScript->deleteLater();
                                           }
                                      });
                              //Start the timer
                                      ptmrScript->start(cuintInterval);
                                  }
                              }
                          }
                          

                          In my JavaScript I call the C++ function:

                          xmleng.createTimer("t1", 50, "simon2.js@testTimer");
                          

                          The JavaScript lambda function, it doesn't get called because the slot doesn't get called:

                          function testTimer() {
                              //All properties area stored as strings, convert to integer
                              var intStageCtr = parseInt(xmleng.getProperty(cstrFormTest, cstrStageCtr));	
                              xmleng.log(0, "testTimer[" + intStageCtr + "]", cstrThis, 316);
                              //Increment stage counter
                              intStageCtr++;
                              //Convert back to string and store
                              xmleng.setProperty(cstrFormTest, cstrStageCtr, String(intStageCtr));
                          }
                          

                          Kind Regards,
                          Sy

                          K 1 Reply Last reply 30 Dec 2021, 08:28
                          0
                          • S SPlatten
                            30 Dec 2021, 08:25

                            @kshegunov , 100% I'm in debug, I'm running in Qt Creator debug and developing. I have a function called createTimer here is the prototype:

                            Q_INVOKABLE void createTimer(const QString& crstrID
                                                        ,const uint cuintInterval
                                                        ,const QString& crstrFunction);
                            

                            The implementation:

                            /**
                             * @brief clsScriptHelper::createTimer
                             * @param crstrID : Constant reference to Timer ID
                             * @param cuintInterval : Constant millisecond interval
                             * @param crstrFunction : Constant reference to JSfile@Function
                             */
                            void clsScriptHelper::createTimer(const QString& crstrID, const uint cuintInterval
                                                             ,const QString& crstrFunction) {
                                if ( crstrID.trimmed().isEmpty() == true || cuintInterval == 0
                                  || crstrFunction.trimmed().isEmpty() == true
                                  || crstrFunction.indexOf(clsXMLnode::msccScriptDelimiter) == -1 ) {
                                //One or more of the parameters are invalid, do nothing
                                    return;
                                }
                                //Get Application instance
                                clsMainWnd* pAppInstance(clsMainWnd::pobjGetAppWnd());
                                if ( pAppInstance == nullptr ) {
                                //Cannot get application instance!
                                    return;
                                }
                                //Look for registered timer
                                QTimer* ptmrScript(pAppInstance->ptmrGetTimer(crstrID));
                                if ( ptmrScript != nullptr ) {
                                //Already registered, do nothing
                                    return;
                                }
                                //No timer locatd with that ID, its safe to proceed
                                QStringList slstScript = crstrFunction.split(clsXMLnode::msccScriptDelimiter);
                                mpScripts* pmpScriptsMap;
                                if ( slstScript.length() == SSF_PARAM_COUNT
                                && (pmpScriptsMap = clsMainWnd::pmpGetScriptsMap()) != nullptr ) {
                                //Look for the script in the scripts map
                                    mpScripts::iterator itrScript(pmpScriptsMap->find(slstScript[SFF_FILE_IDX]));
                                    if ( itrScript != pmpScriptsMap->end() ) {
                                //Found it, build the script up ready for the signal connection
                                        QString strScript(itrScript->second);
                                //Get pointer to script engine
                                        QJSEngine* pobjScriptEng(clsJSON::pobjGetScriptEng());
                                //Create new timer
                                        ptmrScript = new QTimer();
                                //Register the new timer using the supplied ID
                                        pAppInstance->registerTimer(ptmrScript, crstrID);
                                //Add any globals to the script
                                        clsScriptHelper::pobjGetInstance()->addGlobals(pobjScriptEng);
                                //Build up script function call
                                        QString strFile(slstScript[SFF_FILE_IDX])
                                               ,strCall(slstScript[SSF_FUNCTION_IDX])
                                               ,strScriptWithCall(strScript)
                                               ,strFunc(strCall
                                                      + QString(clsDebugService::msccBrktOpen)
                                                      + QString(clsDebugService::msccBrktClose)
                                                      + QString(clsXMLnode::msccScriptFunctionTerminator));
                                         if ( strScriptWithCall.endsWith(clsXMLnode::msccScriptFunctionTerminator) != true ) {
                                             strScriptWithCall += clsXMLnode::msccScriptFunctionTerminator;
                                         }
                                         strScriptWithCall += strFunc;
                                //Create connection to timer and script
                                        QMetaObject::Connection cn = QObject::connect(ptmrScript
                                                                                     ,&QTimer::timeout
                                                                                     ,[pobjScriptEng
                                                                                      ,ptmrScript
                                                                                      ,strFile
                                                                                      ,strFunc
                                                                                      ,strScriptWithCall]() {
                                             QJSValue objResult(pobjScriptEng->evaluate(strScriptWithCall));
                                             QString strError;
                                             if ( objResult.isError() == true ) {
                                                 strError = QString("%1\n%2\n%3").arg(strFile).arg(strFunc)
                                                                                 .arg(objResult.toString());
                                             } else {
                                                 QString strResult(objResult.toString());
                                                 if ( strResult.compare("undefined") != 0 ) {
                                                     strError = strResult;
                                                 }
                                             }
                                             if ( strError.isEmpty() != true ) {
                                                 clsXMLnode* pobjRoot(clsXMLnode::spobjGetRoot());
                                                 emit pobjRoot->error(strError);
                                                 ptmrScript->deleteLater();
                                             }
                                        });
                                //Start the timer
                                        ptmrScript->start(cuintInterval);
                                    }
                                }
                            }
                            

                            In my JavaScript I call the C++ function:

                            xmleng.createTimer("t1", 50, "simon2.js@testTimer");
                            

                            The JavaScript lambda function, it doesn't get called because the slot doesn't get called:

                            function testTimer() {
                                //All properties area stored as strings, convert to integer
                                var intStageCtr = parseInt(xmleng.getProperty(cstrFormTest, cstrStageCtr));	
                                xmleng.log(0, "testTimer[" + intStageCtr + "]", cstrThis, 316);
                                //Increment stage counter
                                intStageCtr++;
                                //Convert back to string and store
                                xmleng.setProperty(cstrFormTest, cstrStageCtr, String(intStageCtr));
                            }
                            
                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 30 Dec 2021, 08:28 last edited by kshegunov
                            #15

                            Put a breakpoint in clsMainWnd::ptmrGetTimer and confirm it gets triggered (the breakpoint).

                            Additionally provide its source, and finally add this to the very beginning of the mentioned function:

                            qDebug() << QThread::currentThread();
                            

                            Read and abide by the Qt Code of Conduct

                            S 1 Reply Last reply 30 Dec 2021, 08:29
                            0
                            • K kshegunov
                              30 Dec 2021, 08:28

                              Put a breakpoint in clsMainWnd::ptmrGetTimer and confirm it gets triggered (the breakpoint).

                              Additionally provide its source, and finally add this to the very beginning of the mentioned function:

                              qDebug() << QThread::currentThread();
                              
                              S Offline
                              S Offline
                              SPlatten
                              wrote on 30 Dec 2021, 08:29 last edited by SPlatten
                              #16

                              @kshegunov , I've stepped through this logic, I expect the initial call to ptmrGetTimer to return nullptr as on first call the timer won't be registered which it isn't. I've stepped through the logic to the line:

                              ptmrScript->start(cuintInterval);
                              

                              No issues except the slot isn't called when the timer expires. And the mentioned debug output.

                              Kind Regards,
                              Sy

                              K 1 Reply Last reply 30 Dec 2021, 08:30
                              0
                              • S SPlatten
                                30 Dec 2021, 08:29

                                @kshegunov , I've stepped through this logic, I expect the initial call to ptmrGetTimer to return nullptr as on first call the timer won't be registered which it isn't. I've stepped through the logic to the line:

                                ptmrScript->start(cuintInterval);
                                

                                No issues except the slot isn't called when the timer expires. And the mentioned debug output.

                                K Offline
                                K Offline
                                kshegunov
                                Moderators
                                wrote on 30 Dec 2021, 08:30 last edited by
                                #17

                                @SPlatten said in Help with timer and JavaScript slot.:

                                @kshegunov , I've stepped through this logic, I expect the initial call to ptmrGetTimer to return nullptr as on first call the timer won't be registered which it isn't.

                                I requested something very specific with a very specific purpose. Please do what I asked and provide the output of the mentioned qDebug call.

                                Read and abide by the Qt Code of Conduct

                                S 1 Reply Last reply 30 Dec 2021, 08:31
                                0
                                • K kshegunov
                                  30 Dec 2021, 08:30

                                  @SPlatten said in Help with timer and JavaScript slot.:

                                  @kshegunov , I've stepped through this logic, I expect the initial call to ptmrGetTimer to return nullptr as on first call the timer won't be registered which it isn't.

                                  I requested something very specific with a very specific purpose. Please do what I asked and provide the output of the mentioned qDebug call.

                                  S Offline
                                  S Offline
                                  SPlatten
                                  wrote on 30 Dec 2021, 08:31 last edited by SPlatten
                                  #18

                                  @kshegunov Adding this now. I've actually changed the code to add:

                                  QThread* pobjCurrThread(QThread::currentThread());
                                  //Create new timer
                                  ptmrScript = new QTimer();
                                  ptmrScript->moveToThread(pobjCurrThread);
                                  qdbg() << "pobjCurrThread: " << pobjCurrThread;
                                  

                                  Output:

                                  pobjCurrThread: QThread(0x600000269c80)
                                  

                                  I have a breakpoint in the lambda function, first line, it doesn't get triggered.

                                  Kind Regards,
                                  Sy

                                  K 1 Reply Last reply 30 Dec 2021, 08:38
                                  0
                                  • S SPlatten
                                    30 Dec 2021, 08:31

                                    @kshegunov Adding this now. I've actually changed the code to add:

                                    QThread* pobjCurrThread(QThread::currentThread());
                                    //Create new timer
                                    ptmrScript = new QTimer();
                                    ptmrScript->moveToThread(pobjCurrThread);
                                    qdbg() << "pobjCurrThread: " << pobjCurrThread;
                                    

                                    Output:

                                    pobjCurrThread: QThread(0x600000269c80)
                                    

                                    I have a breakpoint in the lambda function, first line, it doesn't get triggered.

                                    K Offline
                                    K Offline
                                    kshegunov
                                    Moderators
                                    wrote on 30 Dec 2021, 08:38 last edited by
                                    #19

                                    @SPlatten said in Help with timer and JavaScript slot.:

                                    I have a breakpoint in the lambda function, first line, it doesn't get triggered.

                                    I don't expect it to get trigerred, but again, this is not what I wrote. Here's a reminder:

                                    Put a breakpoint in clsMainWnd::ptmrGetTimer and confirm it gets triggered (the breakpoint).

                                    Additionally provide its source, and finally add this to the very beginning of the mentioned function:
                                    [snip]

                                    Read and abide by the Qt Code of Conduct

                                    S 3 Replies Last reply 30 Dec 2021, 08:40
                                    0
                                    • K kshegunov
                                      30 Dec 2021, 08:38

                                      @SPlatten said in Help with timer and JavaScript slot.:

                                      I have a breakpoint in the lambda function, first line, it doesn't get triggered.

                                      I don't expect it to get trigerred, but again, this is not what I wrote. Here's a reminder:

                                      Put a breakpoint in clsMainWnd::ptmrGetTimer and confirm it gets triggered (the breakpoint).

                                      Additionally provide its source, and finally add this to the very beginning of the mentioned function:
                                      [snip]

                                      S Offline
                                      S Offline
                                      SPlatten
                                      wrote on 30 Dec 2021, 08:40 last edited by
                                      #20

                                      @kshegunov , here is there requested source to the function:

                                      /**
                                       * @brief clsMainWnd::ptmrGetTimer
                                       * @param crstrID : Constant reference to ID to look-up timer
                                       * @return A pointer to the timer or nullptr if not found
                                       */
                                      QTimer* clsMainWnd::ptmrGetTimer(const QString& crstrID) {
                                          mpTimers::iterator itTimer(mmpTimers.find(crstrID));
                                          QTimer* pTimer(nullptr);
                                          if ( itTimer != mmpTimers.end() ) {
                                              pTimer = itTimer->second;
                                          }
                                          return pTimer;
                                      }
                                      

                                      I put breakpoint on first line of this, it does what I expected, nullptr is returned because on the first call the timer hasn't been registered.

                                      Kind Regards,
                                      Sy

                                      S 1 Reply Last reply 30 Dec 2021, 08:51
                                      0
                                      • K kshegunov
                                        30 Dec 2021, 08:38

                                        @SPlatten said in Help with timer and JavaScript slot.:

                                        I have a breakpoint in the lambda function, first line, it doesn't get triggered.

                                        I don't expect it to get trigerred, but again, this is not what I wrote. Here's a reminder:

                                        Put a breakpoint in clsMainWnd::ptmrGetTimer and confirm it gets triggered (the breakpoint).

                                        Additionally provide its source, and finally add this to the very beginning of the mentioned function:
                                        [snip]

                                        S Offline
                                        S Offline
                                        SPlatten
                                        wrote on 30 Dec 2021, 08:45 last edited by
                                        #21

                                        @kshegunov , this is the modified source:

                                                    QThread* pobjCurrThread(QThread::currentThread());
                                            //Create new timer
                                        qdbg() << "Before Timer creation: " << pobjCurrThread;
                                                    ptmrScript = new QTimer(pobjCurrThread);
                                            //Register the new timer using the supplied ID
                                                    pAppInstance->registerTimer(ptmrScript, crstrID);
                                            //Add any globals to the script
                                                    clsScriptHelper::pobjGetInstance()->addGlobals(pobjScriptEng);
                                            //Build up script function call
                                                    QString strFile(slstScript[SFF_FILE_IDX])
                                                           ,strCall(slstScript[SSF_FUNCTION_IDX])
                                                           ,strScriptWithCall(strScript)
                                                           ,strFunc(strCall
                                                                  + QString(clsDebugService::msccBrktOpen)
                                                                  + QString(clsDebugService::msccBrktClose)
                                                                  + QString(clsXMLnode::msccScriptFunctionTerminator));
                                                     if ( strScriptWithCall.endsWith(clsXMLnode::msccScriptFunctionTerminator) != true ) {
                                                         strScriptWithCall += clsXMLnode::msccScriptFunctionTerminator;
                                                     }
                                                     strScriptWithCall += strFunc;
                                            //Create connection to timer and script
                                        qdbg() << "Before QObject::connect: " << QThread::currentThread();
                                        

                                        And the resulting output in debug:

                                        Before Timer creation: QThread(0x60000026c140)
                                        S000000000029E000000010940T08:43:44.084W:QObject: Cannot create children for a parent that is in a different thread.
                                        (Parent is QThread(0x60000026c140), parent's thread is QThread(0x600000004180), current thread is QThread(0x60000026c140)
                                        S000000000030E000000010940T08:43:44.084DL00000661F../clsScriptHelper.cpp[void clsScriptHelper::createTimer]
                                        Before QObject::connect: QThread(0x60000026c140)
                                        

                                        Kind Regards,
                                        Sy

                                        1 Reply Last reply
                                        0
                                        • S SPlatten
                                          30 Dec 2021, 08:40

                                          @kshegunov , here is there requested source to the function:

                                          /**
                                           * @brief clsMainWnd::ptmrGetTimer
                                           * @param crstrID : Constant reference to ID to look-up timer
                                           * @return A pointer to the timer or nullptr if not found
                                           */
                                          QTimer* clsMainWnd::ptmrGetTimer(const QString& crstrID) {
                                              mpTimers::iterator itTimer(mmpTimers.find(crstrID));
                                              QTimer* pTimer(nullptr);
                                              if ( itTimer != mmpTimers.end() ) {
                                                  pTimer = itTimer->second;
                                              }
                                              return pTimer;
                                          }
                                          

                                          I put breakpoint on first line of this, it does what I expected, nullptr is returned because on the first call the timer hasn't been registered.

                                          S Offline
                                          S Offline
                                          SPlatten
                                          wrote on 30 Dec 2021, 08:51 last edited by
                                          #22

                                          @SPlatten , I've reduced the source to just:

                                                      QJSEngine* pobjScriptEng(clsJSON::pobjGetScriptEng());
                                                      QThread* pobjCurrThread(QThread::currentThread());
                                              //Create new timer
                                          qdbg() << "Before Timer creation: " << pobjCurrThread;
                                                      ptmrScript = new QTimer(pobjCurrThread);
                                              //Create connection to timer and script
                                          qdbg() << "Before QObject::connect: " << QThread::currentThread();
                                          
                                          

                                          And the output is still:

                                          Before Timer creation: QThread(0x600000264a00)
                                          S000000000029E000000000768T08:50:01.847W:QObject: Cannot create children for a parent that is in a different thread.
                                          (Parent is QThread(0x600000264a00), parent's thread is QThread(0x6000000042c0), current thread is QThread(0x600000264a00)
                                          S000000000030E000000000768T08:50:01.847DL00000661F../clsScriptHelper.cpp[void clsScriptHelper::createTimer]
                                          Before QObject::connect: QThread(0x600000264a00)
                                          

                                          Kind Regards,
                                          Sy

                                          K 1 Reply Last reply 30 Dec 2021, 08:59
                                          0

                                          12/43

                                          30 Dec 2021, 08:17

                                          31 unread
                                          • Login

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