[solved] Qt 5.4 - Proxy settings for QtWebEngine



  • Hi,

    is there a way to use a proxy with authentication for QtWebEngine module?

    It seems NetworkAccessManager is not used...

    Greetings
    Nando



  • Got it:

    @
    connect(m_webView->page(),
    SIGNAL(proxyAuthenticationRequired(const QUrl &, QAuthenticator *, const QString &)),
    this, SLOT(proxyAuthenticationRequired(const QUrl &, QAuthenticator *, const QString &)));
    @

    And then in the slot something like this:
    @void AirportDisplayer::proxyAuthenticationRequired(const QUrl &, QAuthenticator *auth, const QString &)
    {
    qDebug() << "AirportDisplayer::proxyAuthenticationRequired:";

    auth->setUser(FlightbookManager::instance().pilot()->proxyUsername());
    auth->setPassword(FlightbookManager::instance().pilot()->proxyPassword());
    

    }
    @



  • Thanks for sharing the solution.



  • Can you share how you are setting up the proxy settings (host and port) for the QtWebEngine? My QWebEngineView does not honor the proxy settings made via the QNetworkProxy::setApplicationProxy(proxy) call.
    thanks
    Paul



  • Can you share how you are setting up the proxy settings (host and port) for the QtWebEngine? My QWebEngineView does not honor the proxy settings made via the QNetworkProxy::setApplicationProxy(proxy) call.
    thanks
    Paul



  • Hi Paul.
    i do it like this:

    In my class i have a member of:
    @
    QNetworkProxy m_networkProxy;
    @

    in constructor of my class i create the QNetworkAccessManager:
    @
    m_networkAccessManager = new QNetworkAccessManager;
    @

    and the after loading proxy host, port,username and password i set it like this:
    @
    if (m_proxyEnabled) {
    m_networkProxy.setType(QNetworkProxy::HttpProxy);
    m_networkProxy.setHostName(m_ProxyHost);
    m_networkProxy.setPort(m_ProxyPort);
    m_networkProxy.setUser(m_ProxyUsername);
    m_networkProxy.setPassword(m_ProxyPassword);
    } else {
    m_networkProxy.setType(QNetworkProxy::NoProxy);
    }

    // Apply the proxy to the QNetworkAccessManager
    QNetworkProxy::setApplicationProxy(m_networkProxy);
    m_pNetworkAccessManager->setProxy(m_networkProxy);
    

    @

    Hope that helps ;-)

    Greetings
    Nando



  • Hi Paul.
    i do it like this:

    In my class i have a member of:
    @
    QNetworkProxy m_networkProxy;
    @

    in constructor of my class i create the QNetworkAccessManager:
    @
    m_networkAccessManager = new QNetworkAccessManager;
    @

    and the after loading proxy host, port,username and password i set it like this:
    @
    if (m_proxyEnabled) {
    m_networkProxy.setType(QNetworkProxy::HttpProxy);
    m_networkProxy.setHostName(m_ProxyHost);
    m_networkProxy.setPort(m_ProxyPort);
    m_networkProxy.setUser(m_ProxyUsername);
    m_networkProxy.setPassword(m_ProxyPassword);
    } else {
    m_networkProxy.setType(QNetworkProxy::NoProxy);
    }

    // Apply the proxy to the QNetworkAccessManager
    QNetworkProxy::setApplicationProxy(m_networkProxy);
    m_pNetworkAccessManager->setProxy(m_networkProxy);
    

    @

    Hope that helps ;-)

    Greetings
    Nando



  • Hi Nando
    I tried your suggestions but it did not work.
    I have the following class and associated methods:

    @
    class LoginWebEngineView : public QWebEngineView
    {
    Q_OBJECT

    public:
    explicit LoginWebEngineView(QWidget *parent = 0, QSettings *settings = 0);
    ~LoginWebEngineView();
    void setProxy();

    private:
    QSettings* m_pSettings;
    QNetworkProxy m_networkProxy;
    QNetworkAccessManager *m_pNetworkAccessManager;
    };

    LoginWebEngineView::LoginWebEngineView(QWidget *parent, QSettings *settings) :
    QWebEngineView(parent)
    {
    m_pSettings = settings;
    m_pNetworkAccessManager = new QNetworkAccessManager;

    this->setParent(parent);
    setProxy();
    

    }

    void
    LoginWebEngineView::setProxy() {
    if (m_pSettings->value("proxy_enabled", false).toBool()) {
    QString host = m_pSettings->value("proxy_host", "").toString();
    quint16 port = m_pSettings->value("proxy_port", 0).toUInt();
    QString user = m_pSettings->value("proxy_user", "").toString();
    QString password = m_pSettings->value("proxy_pass", "").toString();

        m_networkProxy.setType(QNetworkProxy::HttpProxy);
        m_networkProxy.setHostName(host);
        m_networkProxy.setPort(port);
        m_networkProxy.setUser(user);
        m_networkProxy.setPassword(password);
    } else {
        m_networkProxy.setType(QNetworkProxy::NoProxy);
    }
    
    // Apply the proxy to the QNetworkAccessManager
    QNetworkProxy::setApplicationProxy(m_networkProxy);
    m_pNetworkAccessManager->setProxy(m_networkProxy);
    

    }

    @

    If you see anything obvious, please let me know.
    thanks
    Paul



  • Hi Nando
    I tried your suggestions but it did not work.
    I have the following class and associated methods:

    @
    class LoginWebEngineView : public QWebEngineView
    {
    Q_OBJECT

    public:
    explicit LoginWebEngineView(QWidget *parent = 0, QSettings *settings = 0);
    ~LoginWebEngineView();
    void setProxy();

    private:
    QSettings* m_pSettings;
    QNetworkProxy m_networkProxy;
    QNetworkAccessManager *m_pNetworkAccessManager;
    };

    LoginWebEngineView::LoginWebEngineView(QWidget *parent, QSettings *settings) :
    QWebEngineView(parent)
    {
    m_pSettings = settings;
    m_pNetworkAccessManager = new QNetworkAccessManager;

    this->setParent(parent);
    setProxy();
    

    }

    void
    LoginWebEngineView::setProxy() {
    if (m_pSettings->value("proxy_enabled", false).toBool()) {
    QString host = m_pSettings->value("proxy_host", "").toString();
    quint16 port = m_pSettings->value("proxy_port", 0).toUInt();
    QString user = m_pSettings->value("proxy_user", "").toString();
    QString password = m_pSettings->value("proxy_pass", "").toString();

        m_networkProxy.setType(QNetworkProxy::HttpProxy);
        m_networkProxy.setHostName(host);
        m_networkProxy.setPort(port);
        m_networkProxy.setUser(user);
        m_networkProxy.setPassword(password);
    } else {
        m_networkProxy.setType(QNetworkProxy::NoProxy);
    }
    
    // Apply the proxy to the QNetworkAccessManager
    QNetworkProxy::setApplicationProxy(m_networkProxy);
    m_pNetworkAccessManager->setProxy(m_networkProxy);
    

    }

    @

    If you see anything obvious, please let me know.
    thanks
    Paul



  • Hi,
    i see you have enhirted your class from QWebEngineView.
    Try to inherit your class just from QObject and add a member for the
    QWebEngineView which gets instantiated before the set proxy stuff:
    @
    QWebEngineView* m_webEngineView;
    @

    Maybe QWebEngine is looking for proxy settings while creation and there are no proxy stettings at construction time.

    Greetings
    Nando



  • Hi,
    i see you have enhirted your class from QWebEngineView.
    Try to inherit your class just from QObject and add a member for the
    QWebEngineView which gets instantiated before the set proxy stuff:
    @
    QWebEngineView* m_webEngineView;
    @

    Maybe QWebEngine is looking for proxy settings while creation and there are no proxy stettings at construction time.

    Greetings
    Nando



  • That did not help. My dialog is created in QtCreator, is it possible that this could be part of the issue?
    thanks
    Paul



  • That did not help. My dialog is created in QtCreator, is it possible that this could be part of the issue?
    thanks
    Paul



  • @pcushman
    Hi, have you solved your proxy problem now? Because I meet the same question as you.

    Thanks,
    Peng


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.