I have a ui
with a QPushButton
and a QLabel
. I would like the QLabel
to be displayed in full screen on my laptop whenever I push that button.
I tried the following code:
void MainWindow::on_fullScreenBtn_clicked()
{
// ui->myImage is a QLabel*
ui->myImage->setText("going full screen");
ui->myImage->showMaximized();
ui->myImage->QWidget::showFullScreen();
}
I can see this function is executed, but it doesn't display my label in full screen. What am I doing incorrectly/missing?
I am using Qt Creator 3.5.1 and made my GUI using the built-in graphical interface.
The problem is that your QLabel
is part of another widget and therefore displayed as a child widget instead due to the default Qt::Widget
flag:
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 alsoQt::Window
andQt::SubWindow.
Therefore, the solution is to change the flag to Qt::Window
manually as follows:
ui->myImage->setWindowFlag(Qt::Window);
Pre Qt 5.9 users can use the following:
ui->myImage->setWindowFlags(ui->myImage->windowFlags() | Qt::Window);
Note that you should call showFullScreen
after you changed the window flags as documented by Qt:
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..
Undo maximisation
The window flag should be removed to undo the maximisation. This can be done as follows:
ui->myImage->setWindowFlag(Qt::Window, false);
ui->myImage->show();
Pre Qt 5.9 users can use the following:
ui->myImage->setWindowFlags(ui->myImage->windowFlags() & ~Qt::Window);
ui->myImage->show();
Note that the call to show
is needed as pointed out above.