4

I am using this code to toggle a window from normal mode to fullscreen:

void MainWindow::on_action_Fullscreen_triggered()
{
    showFullScreen();
}

How to return back to normal mode when I trigger this action again?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
GeoMint
  • 169
  • 1
  • 13

2 Answers2

7

The answer of LogicStuff is almost perfect but it does not account for whether the window was maximized or not. Personally I always use this little snippet:

void main_window::toggle_fullscreen()
{
    isFullScreen() ?
        ((was_maximized_) ? showMaximized() : showNormal()), ui_->menu_view_toggle_fullscreen->setIcon(QIcon(":/fullscreen_enter")) :
        ((was_maximized_ = isMaximized()), showFullScreen(), ui_->menu_view_toggle_fullscreen->setIcon(QIcon(":/fullscreen_exit")));
}

Since showFullScreen() also affects the isMaximized() return value, we have to save it somewhere (was_maximized_) before going full screen.

S. Broekman
  • 91
  • 1
  • 4
5

After a minute session with Qt's reference:

void MainWindow::on_action_Fullscreen_triggered()
{
    isFullScreen() ? showNormal() : showFullScreen();
}
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • This will always exit from full-screen into non-maximized mode, even if the windows was maximized before entering full-screen. – Violet Giraffe Feb 05 '23 at 11:53