1.需求描述
将播放窗口全屏显示,将工具栏边框等隐藏在后面;然后退出全屏通过ESC返回按钮;
2.实现方法
定义一个全屏的容器类,继承于QWidget;全屏时,去掉窗口的父窗口,然后全屏显示,不去掉父窗口指针无法全屏;按下ESC按钮后,通过keyPressEvent(QKeyEvent* pEvent);函数,将父窗口重新设置回去;
#ifndef FULLSCREEN_H #define FULLSCREEN_H #include <QWidget> #include <QKeyEvent> class FullScreen : public QWidget { Q_OBJECT public: FullScreen(QWidget *parent); ~FullScreen(); void SetFullScreen(); void keyPressEvent(QKeyEvent* pEvent); private: QWidget* parentwidget=NULL; }; #endif // FULLSCREEN_H
#include "FullScreen.h" #include <QLayout> FullScreen::FullScreen(QWidget *parent) : QWidget(parent) { } FullScreen::~FullScreen() { } void FullScreen::SetFullScreen() { parentwidget = parentWidget(); parentWidget()->layout()->removeWidget(this); setParent(NULL); showFullScreen(); raise(); } void FullScreen::keyPressEvent(QKeyEvent* pEvent) { if (!pEvent->modifiers()) { if (pEvent->key() == Qt::Key_Escape &&isFullScreen()) { hide();//避免一闪而过 showNormal(); setParent(parentwidget); parentwidget->layout()->addWidget(this); show(); pEvent->accept(); return; } } QWidget::keyPressEvent(pEvent); }
标签:FullScreen,Qt,parentwidget,pEvent,ESC,全屏,QWidget,include From: https://www.cnblogs.com/bclshuai/p/18460744