Important: Please read the Qt Code of Conduct -

Proper way to restore state/geometry on application start?

  • My QWidgets application (MainWindow with a central widget and surrounding dock widgets) uses saveGeometry() and saveState() to store window layout in QSettings.. I've added menu options for "Save Layout" and "Restore Layout" and they work just fine.

    The problem is, I want to restore layout on application startup, and it doesn't seem to quite work. I'm unsure where the best place to call it is. It seems my application loads, and things are "close" to where they were in terms of size and position, but not quite.. Once the application is loaded and the main window is visible, if the user chooses the "Restore Layout" menu option, the layout is restored properly. But I don't want the user to have to do this by hand once the application is open, it should be automatic. I tried extending the showEvent to call my restore layout method, but that didn't work.

    EDIT: I have found that if I the main window is not "maximized" then my automatic restoration of geometry and state (called just prior to win->show() ) works properly.. However, if the saved state is based on a maximized main-window, then I encounter my problems.

  • Lifetime Qt Champion


    Don't restore prior to show. You should rather trigger the restoration after that. One way would be to start a single shot QTimer at the end of the constructor with a 0 delay. That way the restoration will happen when the event loop start running and you should get better results.

  • brilliant! that seems to work well.

    thank you!

Log in to reply