Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. How to find QWebFrame calling C++ API from Javascript
Forum Updated to NodeBB v4.3 + New Features

How to find QWebFrame calling C++ API from Javascript

Scheduled Pinned Locked Moved Qt WebKit
12 Posts 4 Posters 6.3k 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.
  • W Offline
    W Offline
    willc
    wrote on 10 Jan 2011, 11:25 last edited by
    #1

    Hi,
    new here - working on a port of an application from Webkit/GTK to Webkit/QT 4.7.

    is it possible to find a reference to the frame that is executing a JS call to a custom QObject API in C++ via slot function?

    the page has multiple frames which may be calling the C++ API. I would like to store the frame reference so that I can call frame->evaluateJavaScript() later.

    tried using page->currentFrame() but this returns the frame with focus, not the frame executing the JS.

    any help would be appreciated,
    thanks.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      benjamin.poulain
      wrote on 10 Jan 2011, 15:32 last edited by
      #2

      I cannot think of any direct way. But objects are added by frames right? It is convoluted but couldn't you have a caller-proxy objects that keep a reference to the frame it has been added, and pass that as argument to the object executing javascript?

      1 Reply Last reply
      0
      • W Offline
        W Offline
        willc
        wrote on 10 Jan 2011, 16:07 last edited by
        #3

        Hi Benjamin - thanks for the suggestion.

        I would need to create a new custom qobject for each new frame and use these to store the frame reference. The object would have to be deleted on a frame delete - do you know if there a signal/event for a frame delete, I can't see any in the docs?

        I seen there is an 'own' parameter for QWebFrame::addToJavaScriptWindowObject() to set the ownership - maybe if I set this to script then the delete would be handled?

        1 Reply Last reply
        0
        • B Offline
          B Offline
          benjamin.poulain
          wrote on 10 Jan 2011, 16:11 last edited by
          #4

          Yep, that is what I would do, set the ownership to the script engine itself.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DavidGOrtega
            wrote on 11 Jan 2011, 15:38 last edited by
            #5

            Adding the QObject using ScriptOwnership is really a bad idea, the destructor is never called!! I'm figuring that this is not working at least in Qt 4.5.3

            1 Reply Last reply
            0
            • B Offline
              B Offline
              benjamin.poulain
              wrote on 11 Jan 2011, 16:13 last edited by
              #6

              [quote author="DavidGOrtega" date="1294760299"]Adding the QObject using ScriptOwnership is really a bad idea, the destructor is never called!! I'm figuring that this is not working at least in Qt 4.5.3[/quote]

              I know about at least one big project using ScriptOwnership with 4.7 for security check on tear down. I think they would have complained if that was not working :)

              1 Reply Last reply
              0
              • W Offline
                W Offline
                willc
                wrote on 11 Jan 2011, 18:02 last edited by
                #7

                I will test in 4.7 and let you know..

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  willc
                  wrote on 12 Jan 2011, 17:34 last edited by
                  #8

                  fyi - using ScriptOwnership worked ok for me for simple test with 4.7.

                  ie. frame->addToJavaScriptWindowObject( "objectName", pObject, QScriptEngine::ScriptOwnership );

                  pObject gets deleted before frame is deleted.

                  1 Reply Last reply
                  0
                  • W Offline
                    W Offline
                    willc
                    wrote on 13 Jan 2011, 15:53 last edited by
                    #9

                    after some further test I did find something funny using QScriptEngine::ScriptOwnership - on a page reload the object I attached to the main frame gets garbage collected!

                    as an alternative I set the custom object parent to the frame and use default ownership when calling addToJavaScriptWindowObject(). This way the object gets deleted when the frame is deleted which is what I want.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dangelog
                      wrote on 13 Jan 2011, 16:31 last edited by
                      #10

                      [quote author="willc" date="1294934025"]after some further test I did find something funny using QScriptEngine::ScriptOwnership - on a page reload the object I attached to the main frame gets garbage collected!

                      as an alternative I set the custom object parent to the frame and use default ownership when calling addToJavaScriptWindowObject(). This way the object gets deleted when the frame is deleted which is what I want.
                      [/quote]

                      Or, you can call addToJavaScriptWindowObject in a slot connected to the javaScriptWindowObjectCleared signal.

                      Software Engineer
                      KDAB (UK) Ltd., a KDAB Group company

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        benjamin.poulain
                        wrote on 13 Jan 2011, 16:58 last edited by
                        #11

                        @willc, yep, that is intended. Custom objects are not supposed to live past the lifetime of the content. You can do as peppe says to get them from page to page.

                        1 Reply Last reply
                        0
                        • W Offline
                          W Offline
                          willc
                          wrote on 13 Jan 2011, 17:08 last edited by
                          #12

                          yeah - I am doing that too..

                          does anyone know the expected behaviour when an new frame (iframe) is created?

                          I have connected a function to the page "frameCreated" signal - this is called for the new frame and I call newFrame->addToJavaScriptWindowObject() to add my custom object.

                          I also connect to the javaScriptWindowObjectCleared signal for the new frame.

                          Sometimes this signal goes off immediately resulting in a second call to addToJavaScriptWindowObject() - is this expected?

                          1 Reply Last reply
                          0

                          8/12

                          12 Jan 2011, 17:34

                          • Login

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