Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. C++ pointer to WebEngineView in QML
QtWS25 Last Chance

C++ pointer to WebEngineView in QML

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 2 Posters 4.3k Views
  • 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
    whitecastleroad
    wrote on last edited by
    #1

    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
    }
    }
    @

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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.

      157

      1 Reply Last reply
      0
      • W Offline
        W Offline
        whitecastleroad
        wrote on last edited by
        #3

        Thanks. This is exactly what I wound up doing. It worked okay.

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Glad to hear that :)
          Can you please post how you did you pass the JS Callback ?
          It would be helpful to others too.

          157

          1 Reply Last reply
          0
          • W Offline
            W Offline
            whitecastleroad
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              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.

              157

              1 Reply Last reply
              0
              • W Offline
                W Offline
                whitecastleroad
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0

                • Login

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