Setting User And Password For Qt WebView & WebEngine Diectly
-
@Pablo-J-Rogina
Hi
Thank you for replying and helping.
I tried using QNetworkProxyFactory:class ProxyFactory : public QNetworkProxyFactory
{
public:
QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query)
{
Q_UNUSED(query)QNetworkProxy proxy; proxy.setType(QNetworkProxy::HttpProxy); // set proxy variables here return QList<QNetworkProxy>() << proxy; }
};
and then:
QNetworkProxyFactory::setApplicationProxyFactory(new ProxyFactory());but this doesn't work. I mean it proxies QNetworkAccessManager and stuff but it is not what I want.
I want the webview to use this proxy and not raise a dialog and ask the user for credentials. -
@shahriar25 maybe the trick here is that QWebPage within your QWebView is using its own QNetworkAccessManager, so perhaps by doing:
myWebView->page()->setNetworkAccessManager(yourQNAM)
will cause the webview to use your QNAM with your proxy settings.
-
@Pablo-J-Rogina
Well see that is the problem. I'm working with qml and using webview in it -
@shahriar25 are you saying you're using this QML WebView component? If so, on one side you should have stated your issue better :-) and you may need to work with the underlying QNetworkAccessManager of QML, see this post as a guideline.
-
@Pablo-J-Rogina
Hi. I'm sorry for late reply
I'm sorry if I left out using qml. It wasn't intentional
I tried this:class QmlNAMFactory : public QQmlNetworkAccessManagerFactory
{
virtual QNetworkAccessManager *create(QObject *parent)
{
QNetworkAccessManager *nam = new QNetworkAccessManager(parent);
QNetworkProxy proxy;proxy.setType(QNetworkProxy::HttpProxy); // proxy info here nam->setProxy(proxy); return nam; }
};
QQmlApplicationEngine engine;
engine.setNetworkAccessManagerFactory(new QmlNAMFactory);but this has no effect on webview. It doesn't change a thing
I'm sorry the function was returning nullptr I edited it. I did that just to text the effect -
Isn't there anything that I can do?
-
Hi.
Did you solve the problem?
I am hitting the same problem, i need to set a header to every QML WebView request.
Setting the network access manager on
QQmlApplicationEngine
does not callMyNetworkAccessManagerFactory::create
.
Seems WebView is not usingQNetworkAccessManager
at all.Is this possible at all?
thanks for hints.
-
Hi.
Did you solve the problem?
I am hitting the same problem, i need to set a header to every QML WebView request.
Setting the network access manager on
QQmlApplicationEngine
does not callMyNetworkAccessManagerFactory::create
.
Seems WebView is not usingQNetworkAccessManager
at all.Is this possible at all?
thanks for hints.
@i-n-g-o Hi.Do you solve problem?
-
Hi.
Did you solve the problem?
I am hitting the same problem, i need to set a header to every QML WebView request.
Setting the network access manager on
QQmlApplicationEngine
does not callMyNetworkAccessManagerFactory::create
.
Seems WebView is not usingQNetworkAccessManager
at all.Is this possible at all?
thanks for hints.
@i-n-g-o it does
-
The first thing to do is to create a network access Manager factory class
NetworkAccessManagerFactory.cpp
#include "networkaccessmanagerfactory.h"
QNetworkAccessManager *NetworkAccessManagerFactory::create(QObject *parent)
{QNetworkAccessManager *manager = new QNetworkAccessManager(parent);
return manager;
}NetworkAccessManagerFactory.h
#ifndef NETWORKACCESSMANAGERFACTORY_H
#define NETWORKACCESSMANAGERFACTORY_H
#include "customnetworkaccessmanager.h"
#include <QNetworkAccessManager>
#include <QNetworkProxy>
#include <QQmlEngine>
#include <QQmlNetworkAccessManagerFactory>
#include <QtNetwork>class NetworkAccessManagerFactory : public QQmlNetworkAccessManagerFactory
{public:
QNetworkAccessManager *create(QObject *parent) override;private:
};
#endif // NETWORKACCESSMANAGERFACTORY_H
Then finally add to qml side by writing this in the main.cpp
NetworkAccessManagerFactory customNam;
QQmlApplicationEngine engine; engine.setNetworkAccessManagerFactory(&customNam);
-
you can also check this as well
https://code.qt.io/cgit/qt/qtdeclarative.git/tree/examples/qml/networkaccessmanagerfactory?h=5.15