Setting User And Password For Qt WebView & WebEngine Diectly
-
wrote on 3 Feb 2018, 18:48 last edited by
Hi. I wanted to set user and password for Qt WebView & WebEngine directly. I have my own VPS and I have setup HttpProxy on it. My application has a web browser and I want that web browser to use the http proxy. I've tried setting application proxy but the web view raises a dialog asking for user and password. I want to set user and password in the code so that users won't have to deal with the dialog.
-
wrote on 5 Feb 2018, 14:13 last edited by
Any Ideas?
-
wrote on 5 Feb 2018, 17:29 last edited by
@shahriar25 are you talking about user/password for a proxy, so your QWebView application can traverse such proxy, which requires authentication, and reach the desired destination?
If so, I imagine you may need to rely on using a QNetworkProxyFactory that once configured properly (for instance, to use the system proxy's settings) is then set to the QNetworkAccessManager that makes the connection -
wrote on 5 Feb 2018, 19:38 last edited by shahriar25 2 May 2018, 19:40
@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. -
wrote on 6 Feb 2018, 15:15 last edited by
@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.
-
wrote on 6 Feb 2018, 18:02 last edited by
@Pablo-J-Rogina
Well see that is the problem. I'm working with qml and using webview in it -
wrote on 6 Feb 2018, 18:30 last edited by
@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.
-
wrote on 8 Feb 2018, 13:25 last edited by shahriar25 2 Aug 2018, 16:32
@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 -
wrote on 13 Feb 2018, 18:54 last edited by
Isn't there anything that I can do?
-
wrote on 15 Apr 2019, 16:52 last edited by
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.
wrote on 12 Feb 2024, 11:08 last edited by@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.
wrote on 12 Feb 2024, 14:07 last edited by@i-n-g-o it does
-
wrote on 12 Feb 2024, 14:16 last edited by
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);
-
wrote on 13 Feb 2024, 10:09 last edited by
you can also check this as well
https://code.qt.io/cgit/qt/qtdeclarative.git/tree/examples/qml/networkaccessmanagerfactory?h=5.15