在qt5.8 webengine中更改浏览器名称

时间:2017-03-27 04:17:56

标签: c++ qt http-headers user-agent

我想将我在qt5.8中制作的webbrowser命名为一些自定义名称,例如"我的WebBrowser"使用http用户代理。我当前的用户代理是Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.8.0 Chrome/53.0.2785.148 Safari/537.36。我应该如何更改它,以便我的浏览器名称被标识为"我的WebBrowser"而不是Qt5.8.0或Chrome53。

    class WebView:public QObject{
        void acceptFullScreen(QWebEngineFullScreenRequest request){
            request.accept();
        }

    public:
        char* home_page;
        QWebEngineView* view=new QWebEngineView();
        WebView(char* page=(char*)"https://google.com"){
            this->home_page=page;
            createWebView();
            this->view->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled,true);
            connect(this->view->page(),&QWebEnginePage::fullScreenRequested,this,&WebView::acceptFullScreen);
            this->view->page()->profile()->setHttpUserAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.8.0 MyBrowser/1.0 Chrome/53.0.2785.148 Safari/537.36");
        }
        void createWebView(){
            this->view->load(QUrl(this->home_page));
        }
        QWebEngineView* returnView(){
            return this->view;
        }
        void home(){
            this->view->load(QUrl(this->home_page));
        }
        void spinnerStarted(){
            QWidget* widget=(QWidget*)this->view->parent();
            QTabWidget* tabwidget=(QTabWidget*)widget->parent();
            std::cout<<tabwidget<<std::endl;
        }
    };

1 个答案:

答案 0 :(得分:0)

您可以使用QWebEngineProfile::setHttpUserAgent设置自定义用户代理。