How to find QWebFrame calling C++ API from Javascript
-
wrote on 10 Jan 2011, 16:07 last edited by
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?
-
wrote on 10 Jan 2011, 16:11 last edited by
Yep, that is what I would do, set the ownership to the script engine itself.
-
wrote on 11 Jan 2011, 15:38 last edited by
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
-
wrote on 11 Jan 2011, 16:13 last edited by
[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 :)
-
wrote on 11 Jan 2011, 18:02 last edited by
I will test in 4.7 and let you know..
-
wrote on 12 Jan 2011, 17:34 last edited by
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.
-
wrote on 13 Jan 2011, 15:53 last edited by
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.
-
wrote on 13 Jan 2011, 16:31 last edited by
[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.
-
wrote on 13 Jan 2011, 16:58 last edited by
@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.
-
wrote on 13 Jan 2011, 17:08 last edited by
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?
12/12