[Solved] How set user-agent in QWebView?

  • How can I change HTTP_USER_AGENT used by QWebView?
    In half-measure I can change name and version of application, such as
    @ app.setApplicationName(QString("Chrome"));
    but I can not change version of WebKit and OS. Ideally I would like to completely change HTTP_USER_AGENT and HTTP_ACCEPT_LANGUAGE to arbitrary values ​​for QWebView.

    I can download individual pages by using the
    @void load ( const QNetworkRequest & request, QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation, const QByteArray & body = QByteArray() )@
    and in QNetworkRequest change setRawHeader
    but how do I replace HTTP_USER_AGENT for any user action?
    As alternative i can make proxy, which changes HTTP_USER_AGENT and HTTP_ACCEPT_LANGUAGE

    Volker, thanks, it work
    My implementation:
    @class QWebPageChrome : public QWebPage
    QString userAgentForUrl ( const QUrl & url ) const;

    class QWebPageFirefox : public QWebPage
    QString userAgentForUrl ( const QUrl & url ) const;

    @QString QWebPageChrome::userAgentForUrl ( const QUrl & url ) const
    return "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
    QString QWebPageFirefox::userAgentForUrl ( const QUrl & url ) const
    return "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";

    @ QWebView *chromeView = new QWebView(this);
    QWebView *firefoxView = new QWebView(this);
    QWebPageChrome *pageChrome= new QWebPageChrome();
    QWebPageFirefox *pageFirefox= new QWebPageFirefox();

  • Have a look at "QWebPage::userAgentForUrl() ":http://doc.qt.nokia.com/4.7/qwebpage.html#userAgentForUrl

  • I just wanted to thank you guys! Volker, for sharing your knowledge and Pawelitel for sharing his implementation. Works like a charm! :)

Log in to reply

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