Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    C++ pointer to WebEngineView in QML

    QML and Qt Quick
    2
    7
    3961
    Loading More Posts
    • 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
      whitecastleroad last edited by

      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 Reply Quote 0
      • p3c0
        p3c0 Moderators last edited by

        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 Reply Quote 0
        • W
          whitecastleroad last edited by

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

          1 Reply Last reply Reply Quote 0
          • p3c0
            p3c0 Moderators last edited by

            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 Reply Quote 0
            • W
              whitecastleroad last edited by

              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 Reply Quote 0
              • p3c0
                p3c0 Moderators last edited by

                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 Reply Quote 0
                • W
                  whitecastleroad last edited by

                  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 Reply Quote 0
                  • First post
                    Last post