0

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));
    }
}
Anmol Gautam
  • 949
  • 1
  • 12
  • 27

3 Answers3

2

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...

cbuchart
  • 10,847
  • 9
  • 53
  • 93
  • I found your answer useful , meanwhile I found other method to do the same (which i posted as answer) , could you please tell me if there is some mistake in my posted answer, Thanks – Anmol Gautam Mar 29 '17 at 12:08
  • Your're right! I forgot that one (the [`showFullScreen`](http://doc.qt.io/qt-5/qwidget.html#showFullScreen), which is basically the second option using the full geometry of the desktop, although fullscreen widgets may be handled differently (see documentation about X11). – cbuchart Mar 29 '17 at 12:20
1

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);
        }
Anmol Gautam
  • 949
  • 1
  • 12
  • 27
0

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.

user16217248
  • 3,119
  • 19
  • 19
  • 37
jimmy cao
  • 1
  • 1