Important: Please read the Qt Code of Conduct -

QWebKit Setting Up User Agent

  • My application uses QWebKit on Ubuntu Linux. I need to setup a User Agent with a prefix and then add an emulated browser agent string.

    I figured out how to setup the User Agent by overloading QWebPage::userAgentForUrl. I have the User Agent prefix setup already but now I want to add the emulated browser agent string to this prefix.

    I found that in MAC using WebKit::WebView the emulated browser agent is setup by the following:

    WebView myWebView;
    NSString *uagnt = [myWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

    I know the QWebKit uses WebKit as a backend so this must be possible in Qt WebKit. How can I go about doing this in QWebView / QWebPage?

  • Lifetime Qt Champion


    From the looks of it, you can create a QWebPage subclass and re-implement QWebPage::userAgentForUrl.

    Hope it helps

  • @sgaist Thanks for the reply! I have already created a subclass and reimplemented QWebPage::userAgentForUrl so I got that part covered.

    The main question I had was what is the Qt equivalent of:


    In Qt is there a way I can extract the "navigator.userAgent" JavaScript as seen in this code example using WebView on MAC:

    WebView myWebView;
    NSString *uagnt = [myWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

  • Lifetime Qt Champion

    I may have misunderstood your problem.

    Can you detail a bit more what you want to do with that information and where you would like to extract it ?

  • @sgaist Sorry I should have been more clear to begin with. Basically all I want to do is extract the following string in QtWebKit by evaluating JavaScript:


    I want to set up our user agent with a predefined prefix plus the JavaScript evaluated "navigator.userAgent".

    So I have already subclassed QWebPage::userAgentForUrl and have the prefix set. Now the last piece of the puzzle is adding "navigator.userAgent" by evaluating JavaScript.

    I need to do this for SAML authentication testing.

    As I illustrated before in MAC this is done by:

    WebView myWebView;
    NSString *uagnt = [myWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

    How can I achieve this in QtWebKit? Do I achieve this using QWebView, QWebPage, or QWebFrame?

    Hope I was clearer this time around.

  • Evaluating navigator.userAgent returns whatever string is returned by QWebPage::userAgentForUrl. If you want to evaluate arbitrary JS expressions, use QWebFrame::evaluateJavaScript

  • @konstantin-tokarev
    Is there a way to get the parent's userAgentForUrl value? Such as below?

    // User Agent Web Page
    class UserAgentWebPage : public QWebPage
        QString userAgentForUrl(const QUrl &url) const
            // UserAgent: Prefix
            QString userAgent = QWebPage::userAgentForUrl();
            // Modify userAgent with custom prefixes
            return userAgent;

    My objective is to first obtain QtWebKit's User-Agent and then prefix that with my own custom fields.

    So let me summarize my questions:

    1. How do I obtain the default browser User-Agent string using QtWebKit?

    2. How can I call QWebPage::userAgentForUrl() from it's child?

    Any help would be most appreciated guys as I'm at a roadblock at the moment.

  • Is there a way to get the parent's userAgentForUrl value? Such as below?

    Yes it's possible

  • However it might be enough for your purposes to set application name and version on your QApplication, this information will be displayed in UA string

Log in to reply