Qt框架程序,全屏主窗口很简单,只需要调用主窗口对象的showFullScreen函数就可以了。
子窗口想要全屏的话,稍微复杂一些。因为Qt文档中明确表示,showFullScreen只对调用isWindow()返回true,的窗口生效。
所以,全屏时需要将子窗口变为独立窗口。
ui.frame->setWindowFlag(Qt::Window, true);
ui.frame->showFullScreen();
此时,会发现,当电脑连接多个屏幕时,全屏窗口始终显示在主屏幕上。解决方案如下,这样全屏窗口就显示在主窗口所在屏幕了。
ui.frame->windowHandle()->setScreen(screen());
窗口还原代码如下。
ui.frame->setWindowFlag(Qt::Window, false);
ui.frame->showNormal();
细心的同学会发现,全屏时,将鼠标放在任务栏的程序图标上,显示的缩略图是原主窗口隐藏全屏的子窗口后的图像。即,原主窗口仍然显示在桌面上的,只是被全屏的子窗口挡住了。可以调用window()->setVisible(false) 来隐藏主窗口,此时,任务栏上的程序图标会消失。
下面,介绍另一种全屏方案,来避免上述问题。
ui.frame->setParent(nullptr);
ui.frame->move(screen()->geometry().topLeft()); //移动到主窗口所在屏幕
ui.frame->showFullScreen();
还原
ui.frame->showNormal();
ui.frame->setParent(this);
ui.windowLayout->insertWidget(1, ui.frame); //放回原布局中
这样,任务栏上会出现两个图标,且两个顶层窗口都会显示在桌面上。此时再调用window()->setVisible(false),隐藏原主窗口即可。完整代码
if (bFull)
{
ui.frame->setParent(nullptr);
ui.frame->move(screen()->geometry().topLeft());
ui.frame->showFullScreen();
}
else
{
ui.frame->showNormal();
ui.frame->setParent(this);
ui.windowLayout->insertWidget(1, ui.frame);
}
window()->setVisible(!bFull);