Question

I have qml file with a webview in it. I get a reference to that object in c++ but is QObject* Can i cast it to something similar to QWebView from which i can get the QWebPage object and so? qobject_cast<> returns NULL if i try QWebView. It works with cast to QQuickItem but that doesn't really help me much.

Thanks

Was it helpful?

Solution

You can't. The QML WebView element doesn't have a public C++ API. You have two ways to work around this:

  1. Use private API. First, add this to the .pro file:
QT += quick quick-private webkit-private

Then include the private headers:

#include <private/qquickwebview_p.h>

Now you can access the WebView, as long as you get a pointer to it (for example by using QObject::findChildren or by passing the QObject* from QML to C++. Here I simply assume the WebView is the root object, for simplicity):

QQuickWebView * webView = dynamic_cast<QQuickWebView*>(viewer.rootObject());
qDebug() << webView->url();
  1. Use the generic QObject API to access the WebView's properties:
QObject * webView = viewer.rootObject();
qDebug() << webView->property("url");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top