Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 681
qdot Avatar asked Oct 06 '22 16:10

qdot


2 Answers

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();
}
like image 124
Luca Carlon Avatar answered Oct 10 '22 04:10

Luca Carlon


I have modified the previous example. The previous example never goes back to normal screen.

Just copy paste the code and it will run.

        #include <QApplication>
        #include <QPushButton>

        class MyButton : public QPushButton
        {
        public:
           MyButton(QWidget* parent) : QPushButton(parent) {
               m_pParent = parent;
               maxMode = false;
           }
           QWidget * m_pParent;
           bool maxMode;
           Qt::WindowFlags m_enOrigWindowFlags;
           QSize m_pSize;

           void mousePressEvent(QMouseEvent*) {
               if (maxMode== false)
               {
                   m_enOrigWindowFlags = this->windowFlags();
                   m_pSize = this->size();
                    this->setParent(0);
                    this->setWindowFlags( Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
                    this->showMaximized();
                    maxMode = true;
               }
               else
               {
                   this->setParent(m_pParent);
                   this ->resize(m_pSize);
                   this->overrideWindowFlags(m_enOrigWindowFlags);
                   this->show();
                   maxMode =  false;
               }
           }
        };

        int main(int argc, char *argv[])
        {
           QApplication a(argc, argv);

           QWidget mainWidget;
           MyButton button(&mainWidget);
           mainWidget.show();

           return a.exec();
        }
like image 34
Sandeep Sinha Avatar answered Oct 10 '22 02:10

Sandeep Sinha