C++ pointer to WebEngineView in QML
-
I have the following QML:
@
ApplicationWindow { ...Item { anchors.fill: parent WebEngineView { id: currentWebView objectName: "webView" ... } }
}
@I would then like to run javascript inside of the view from c++, ea:
@
QWebEngineView* view = {SOME MAGIC};
view->page()->runJavaScript();
@However, after trying all day, I cannot figure this out :( I have:
@
QObject object = rootObjects().first();
QQuickItem o = object->findChild<QQuickItem>("webView");
if (o) {
QWebEngineView view = qobject_cast<QWebEngineView*>(o);
if (view) {
... never gets here - always NULL
}
}
@ -
Hi,
I think that might not be possible as QQuickWebEngineView is not public. But you can use "QMetaObject::invokeMethod":http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods to call the runJavaScript method after casting it to QQuickItem. But I'm not sure how to pass the JS callback (which "runJavaScript":http://doc.qt.io/qt-5/qml-qtwebengine-webengineview.html#runJavaScript-method method requires )as an argument to invokeMethod.
-
Thanks. This is exactly what I wound up doing. It worked okay.
-
Glad to hear that :)
Can you please post how you did you pass the JS Callback ?
It would be helpful to others too. -
c++
@
QObject *object = rootObjects().first();
QQuickItem o = object->findChild<QQuickItem>("webView");
if (o) {
QMetaObject::invokeMethod(o, "runJavaScriptString", Q_ARG(QVariant, js));
}
@QML:
@
WebEngineView {
id: currentWebView
objectName: "webView"function runJavaScriptString(command) { runJavaScript(command); }
}
@Seems to work for me. If only there was some way to access the javascript console in the view it would be useful. As it is, it has become a nightmare to throw javascript at the view and not be able to get any feedback.
-
bq. As it is, it has become a nightmare to throw javascript at the view and not be able to get any feedback.
I guess the 2'nd parameter of "runJavaScript()":http://doc.qt.io/qt-5/qml-qtwebengine-webengineview.html#runJavaScript-method is meant for that.
-
True, based on my cryptic rant "not be able to get any feedback", however, what I meant really was viewing the javascript console errors. Thanks for your feedback on this and the other "Debugging" post. I will try the experimental feature and post my results on both threads. I appreciate your help.