Search code examples
qtwidgetfullscreenqtgui

Qt Widget temporarily fullscreen


Consider a QWidget, normally a child in some Layout.

Supposed I want to make it fullScreen for a while, then have it return to it's old spot.

QWidget::setFullScreen() requires that the widget needs to be an independent window - any ideas how to work it out?


Solution

  • The simplest way I can see is to reparent to 0. Something like this:

    #include <QApplication>
    #include <QPushButton>
    
    class MyButton : public QPushButton
    {
    public:
       MyButton(QWidget* parent) : QPushButton(parent) {}
    
       void mousePressEvent(QMouseEvent*) {
          this->setParent(0);
          this->showMaximized();
          this->show();
       }
    };
    
    int main(int argc, char *argv[])
    {
       QApplication a(argc, argv);
    
       QWidget mainWidget;
       MyButton button(&mainWidget);
       mainWidget.show();
    
       return a.exec();
    }