Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to set cookies for a WebView in qml
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.
chrisadams last edited by
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);
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.
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?
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.
mibrunin last edited by
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.