Qt Widget temporarily fullscreen

我只是一个虾纸丫 提交于 2019-12-10 14:07:26

问题


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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/12338548/qt-widget-temporarily-fullscreen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!