Anmol Gautam
Anmol Gautam

Reputation: 1020

How to make QWebEngineView go fullscreen?

I have the following code, and I want to make my QWebEngineView (Qt 5.8) to go full screen.

My WebView class is contained in a QTabWidget, so it just fills up the tab, not entire screen.

How can I make it go fullscreen?

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));
    }
}

Upvotes: 0

Views: 4045

Answers (3)

jimmy cao
jimmy cao

Reputation: 1

I find an easy way to fullscreen. My QtWebEngineview is a central widget of the main window, I used ctrl+shift+F11 to toggle the fullscreen.

  1. Enable and connect the signal and slot
    auto settings = m_webEngineView->settings();
    settings->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
    QShortcut *fullscreen = new QShortcut(this);
    fullscreen->setKey(Qt::CTRL + Qt::SHIFT + Qt::Key_F11);
    connect(fullscreen, SIGNAL(activated()), this, SLOT(onFullScreen()));
    
  2. onFullScreen slot, just toggle the QtWdiget window flags
    void HzWindow::onFullScreen(){
       setWindowState(windowState() ^ Qt::WindowFullScreen);
    }
    

That's all I have done.

Upvotes: 0

Anmol Gautam
Anmol Gautam

Reputation: 1020

I have found a way to do this ,so I am answering my own question : I can change the acceptFullScreen function as :

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);
        }

Upvotes: 1

cbuchart
cbuchart

Reputation: 11575

If your widget is inside a tab, then it can't be full screen directly. You have two options:

  • Remove it from the tab when you want to make it fullscreen (for example, if you have a fullscreen button) and make it a standalone widget. Insert it back into the QTabWidget when quitting the fullscreen mode.
  • Make the QTabWidget to fulfill the screen.

In both cases you can use something like this to make it occupy the whole screen:

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

It will fulfill the screen but will keep the taskbar visible (in my experience this is highly recommended, so the user can easily switch to other tasks). If you want to cover it, just use geometry() instead of the availableGeometry() method.

EDIT in both cases the widget will have the windows manager frame. If you want to remove it you may try setting the Qt::FramelessWindowHint flag. Take into consideration that removing frame may also make some actions unavailable (at least on Windows) such as moving, resizing, snapping...

Upvotes: 2

Related Questions