Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set cookies for a WebView in qml
Qt 6.11 is out! See what's new in the release blog

How to set cookies for a WebView in qml

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 5.3k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Currently I am trying to do this by accessing the "QWebView" object. Following is the code that I use:
    @
    QObject* window = getQmlWindow("appWindow"); // returns the top-level qml ApplicationWindow
    QObject* webView = window->findChild<QObject*>("QWebView"); // WebView has an objectName = "QWebView"
    QWebView* qWebView = static_cast<QWebView *>(webView); // Cast QObject to QWebView

    qWebView->page()->networkAccessManager()->setCookieJar(cookies); // cookies is of type QNetworkCookieJar*
    @

    it gives a segmentation fault error while accessing networkAccessManager().

    Am i doing it in the right way? Is there any other ways to set cookies for Qml WebView. I am using QtQuick 5.1 and QtWebKit 3.0.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisadams
      wrote on last edited by
      #2

      Hi,

      I mentioned in IRC:

      [18:09:47] <chriadam> redmars: you're static casting to a QWebView, but I assume that it's a QQuickWebView
      [18:10:20] <chriadam> you can't really do it that way, anyway
      [18:10:29] <chriadam> i'll pastebin some code, one sec.
      [18:13:11] <chriadam> view->engine()->networkAccessManager()->setCookieJar(cookies);
      or alternatively:
      qmlEngine(window->findChild<QObject*>("WebView"))->networkAccessManager()->setCookieJar(cookies);

      I believe that the QML WebView type (registered from a C++ type called QQuickWebView) uses the QML engine's network access manager to access the internet. Thus, you need to set the cookie jar on that QNAM.

      Cheers,
      Chris.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Thanks for the reply Chris.

        I am not able to use the QQuickWebView in the application. Do I need to use any module for that?

        I'm using QWebView as it is mentioned in this page: http://qt-project.org/doc/qt-5.0/qtwebkit/qtwebkit-module.html

        Btw, I tried in the other way also, by setting the cookie QNetworkCookieJar* object in the engine's networkAccessManager. But that doesn't work either. Any help?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          When I check in the Locals window, the type of the WebView object is displayed as QQuickWebViewExperimental. But I am not able to cast WebView object to this type.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mibrunin
            wrote on last edited by
            #5

            Hello redmars,

            what you are trying to do is not supported by the QML WebKit2 API. The WebView element in the QtWebKit 3.0 quick module is not backed by the QWebView class, but to QQuickWebView / QQuickWebViewExperimental as Chris has pointed out already, which are part of the private C++ backend for the QML module.

            You might consider using the QtWebKitWidgets C++ API if that is an option. And / or you may want to follow the development of the new QtWebEngine module (http://qt-project.org/wiki/QtWebEngine).

            I am sorry I can't provide you with a solution for the problem, but unfortunately, what you would like to achieve is not possible with the current QtWebKit 3.0 module / the WebKit 2 api in Qt.

            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