How to determine which chromium version is used by QtWebEngine in Qt 5 at run-time?

Viewed 1814

I couldn't find any function in Qt 5 to determine which chromium version is used by QtWebEngine.

I don't want to hard-code the chromium version in my code because I frequently update my application and the chromium version is usually changed in each version. And also Qt is backward-compatible and it is possible to update it without updating my application.

3 Answers

There is no direct solution but looking at the source code You can see that it is used to set the default user agent:

std::string ContentBrowserClientQt::getUserAgent()
{
    // Mention the Chromium version we're based on to get passed stupid UA-string-based feature detection (several WebRTC demos need this)
    return content::BuildUserAgentFromProduct("QtWebEngine/" QTWEBENGINECORE_VERSION_STR " Chrome/" CHROMIUM_VERSION);
}

So it can be extracted from that data:

QString version;
QString user_agent = QWebEngineProfile::defaultProfile()->httpUserAgent();
for(const QString & text : user_agent.split(" ")){
    if(text.startsWith(QStringLiteral("Chrome/"))){
        version = text.mid(QStringLiteral("Chrome/").length());
    }
}
qDebug().noquote()<< "Qt version:" << QT_VERSION_STR << "chromium version:" << version;

Output:

Qt version: 5.14.2 chromium version: 77.0.3865.129

You can use the runJavaScript method to show a dialog box with the navigator.userAgent Javascript variable, which includes the Chromium version:

QWebEngineView webView;
webView.page()->runJavaScript("alert(navigator.userAgent)");

In my case, the alert box says the following:

Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.15.1 Chrome/80.0.3987.163 Safari/537.36

The interesting part for finding the Chromium version is Chrome/80.0.3987.163, which means that the Chromium version the Qt Web Engine is using is 80.0.3987.163.

If you run QT with the devtools allowed, you can get the chrome version inside the devtools that way :

navigator.appVersion.match(/.*Chrome\/([0-9\.]+)/)[1]

This solution comes from this stack overflow answer.

Related