簡體   English   中英

如何使QWebEngineView全屏顯示[Qt 5.8]

[英]How to make QWebEngineView go fullscreen [Qt 5.8]

我有以下代碼,我想使我的QWebEngineView (Qt 5.8)進入全屏狀態。 我的WebView類位於QTabWidget因此它只是填充選項卡而不是整個屏幕。 如何使其全屏顯示?

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

public:
    char* home_page;
    QWebEngineView* view=new QWebEngineView();
    WebView(char* page=(char*)"file:///home/tarptaeya/Desktop/Crusta_Prototype_python/about.html"){
        this->home_page=page;
        createWebView();
        this->view->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled,true);
        this->view->settings()->setAttribute(QWebEngineSettings::PluginsEnabled,true);
        this->view->settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows,true);
        connect(this->view->page(),&QWebEnginePage::fullScreenRequested,this,&WebView::acceptFullScreen);
    }
    void createWebView(){
        this->view->load(QUrl(this->home_page));
    }
}

如果您的小部件位於選項卡中,則不能直接全屏顯示。 您有兩種選擇:

  • 要使其全屏顯示(例如,如果您具有全屏按鈕),請從選項卡中將其刪除,並使其成為獨立的小部件。 退出全屏模式時,將其重新插入QTabWidget
  • 使QTabWidget完成屏幕。

在這兩種情況下,您都可以使用以下方法使其占據整個屏幕:

// Replace the 0 with the screen index
const auto windowGeometry = qApp->desktop()->availableGeometry(0);
widget.move(windowGeometry.topLeft());
widget.resize(windowGeometry.size());

它將完成屏幕,但將使任務欄保持可見(根據我的經驗,強烈建議這樣做,以便用戶可以輕松切換到其他任務)。 如果要覆蓋它,只需使用geometry()而不是availableGeometry()方法即可。

在兩種情況下, 編輯都將使小部件具有Windows管理器框架。 如果要刪除它,可以嘗試設置Qt::FramelessWindowHint標志。 請注意,移除框架可能還會使某些操作不可用(至少在Windows上如此),例如移動,調整大小,捕捉...

我已經找到一種執行此操作的方法,所以我在回答自己的問題:我可以將acceptFullScreen函數更改為:

void acceptFullScreen(QWebEngineFullScreenRequest request){
        if(request.toggleOn()){
            request.accept();
            QWidget* w=(QWidget*)this->view->parent();
            this->layout=w->layout();
            this->layout->removeWidget(this->view);
            this->view->setParent(0);
            this->view->showFullScreen();
        }
        else{
            request.accept();
            this->layout->addWidget(this->view);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM