我有一个带有QButton
和QLabel
的ui
。我希望每当我按下该按钮时,QLabel
都能在我的笔记本电脑上全屏显示。
我尝试了以下代码:
void MainWindow::on_fullScreenBtn_clicked()
{
// ui->myImage is a QLabel*
ui->myImage->setText("going full screen");
ui->myImage->showMaximized();
ui->myImage->QWidget::showFullScreen();
}
我可以看到这个函数被执行了,但这并没有全屏显示我的标签。我做错了什么/遗漏了什么?
我正在使用 QtCreator 3.5.1 并使用内置的图形界面制作我的 gui。
编辑:变量 myImage 是一个包含图像的 QLabel
问题是您的 QLabel
是另一个小部件的一部分,因此由于默认 Qt::Widget
而显示为子小部件标志:
This is the default type for QWidget
.
Widgets of this type are child widgets if they have a parent, and
independent windows if they have no parent. See also Qt::Window
and Qt::SubWindow.
因此,解决方案是手动将标志更改为Qt::Window
,如下所示:
ui->myImage->setWindowFlag(Qt::Window);
Qt 5.9 之前的用户可以使用以下内容:
ui->myImage->setWindowFlags(ui->myImage->windowFlags() | Qt::Window);
请注意,在将窗口标志更改为 documented by Qt 之后,您应该调用 showFullScreen
:
Note: This function calls setParent() when changing the flags for a
window, causing the widget to be hidden. You must call show() to make
the widget visible again..
撤消最大化
应该删除窗口标志以撤消最大化。这可以按如下方式完成:
ui->myImage->setWindowFlag(Qt::Window, false);
ui->myImage->show();
Qt 5.9 之前的用户可以使用以下内容:
ui->myImage->setWindowFlags(ui->myImage->windowFlags() & ~Qt::Window);
ui->myImage->show();
请注意,如上所述,需要调用 show
。