How can i set the User Agent for child windows with QTWebEngine?

599 views Asked by At

I created my own Webbrowser with QtWebEngine. Now I would like to set the User Agent with this->page()->profile()->setHttpUserAgent(USER_AGENT);. This works well for the main view.

If i create a new myWebEngineView inside the methode createWindow of myWebEngineView the User Agent will be the default User Agent of QtWebEngine.

myWebEngineView is a subclass of QWebEngineView.

How can I set the User Agent for all views?

1

There are 1 answers

0
elsamuko On

You can set the user agent in the newly created window again:

QWebEngineView* WebEngineView::createWindow( QWebEnginePage::WebWindowType type ) {
    QWebEngineView* view = createView(); // your function to create a new tab or sth like this
    view->page()->profile()->setHttpUserAgent( "" );
    view->page()->profile()->setHttpUserAgent( "Mozilla/5.0( YOUR USER AGENT )" );
    return view;
}

The user agent needs to be set twice, else its not transferred to the pages, see also ProfileAdapter::setHttpUserAgent(const QString &userAgent).