Reference to WebView from C++

  • Hi.

    I make little application using QML and it contains WebView component.

    I want to get the reference to QML object WebView from C++.

    My QML is like this:
    WebView {
    objectName: "adil1234"

            preferredWidth: flick.width
            preferredHeight: flick.height
            settings.pluginsEnabled: true
            smooth: true
            anchors {
            onUrlChanged: {
                flick.contentY = 0


    I get the reference to this object in C++ by such method:
    QUrl source("qrc:res/qml/qmlfile.qml");
    QApplication app(argc, argv);

    QDeclarativeView view;
    QObject::connect(view.engine(), SIGNAL(quit()), &app, SLOT(quit()));
    QObject *object = view.rootObject();
    QObject *rect = object->findChild<QObject*>("adil1234");


    The rect is the reference to this object. It's OK but it's type is QObject. Now I need to call webkit related functions. So I write
    QGraphicsWebView* adil = (QGraphicsWebView*)rect;


    QWebView* adil = (QWebView*)rect;

    When I call any function of adil for example adil->url() application crashes. I think such casting is not OK. In this situation how to get the reference of my webview component and work with it like QWebView.


  • Always do qobject_cast when you do casting a QObject derived class.

    @QObject rect = object->findChild<QObject>("adil1234");@

    now I could see that url is a property of WebView and you should be able to access url property with below code

    QVariant urlpath = rect->property("url");
    QString urlstring = urlpath.toString();

    Try this out and let us know if this works.

  • Thanks you for your attention.
    For now my purpose is to get the selected text of WebView so to call rect->selectedText();. In this case rect is QObject and it's neat that it has no selectedText() method. QWebView has selectedText() method. How to get the reference of rect as QWebView (or QGraphicsWebView)?

  • I doubt you can do that...

    When you execute below code.. dose adil have a valid pointer ( other than zero ) ? My guess is it will be zero
    @QObject rect = object->findChild<QObject>("adil1234");
    QWebView* adil = qobject_cast<QWebView>(rect);@

    You can not call selectedText() on WebView because WebView does not have a method with that name. Suppose if it has, you can call it with


  • That's exactly the problem, Vijay.

    I need to call back my C++ code from QML in order to set up a couple of things in the WebView (like setting the background to avoid the background transparency bug on Symbian, disabling the context menu and similar). I do it the usual way, passing the WebView id as an argument from the Component.onCompleted of the WebView in QML.

    C++ accepts and tries to cast it the usual way (the way that works with many other objects, WebView seems to be the exception):

    @void xxx::setup(QObject* browser) {
    QWebView* webBrowser = qobject_cast<QWebView*>(browser);
    qDebug() << browser;
    qDebug() << webBrowser;

    While browser is a QDeclarativeWebView all right, it can't be cast to a QWebView, webBrowser will be a null QObject instead. Do you have any suggestion?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.