save/restore all layout settings



  • I have looked into using QSettings for saving and restoring geometry and state of my QMainWindow. What I have so far is a simple load and save function for the properties mentioned above:

    void MainWindow::saveWindowSettings()
    {
        QSettings settings("MyCompany", "MyApp");
        settings.setValue("geometry", saveGeometry()); 
        settings.setValue("windowState", saveState());
        qDebug() << "Settings saved under:" << settings.fileName();
    }
    
    void MainWindow::loadWindowSettings()
    {
        QSettings settings("MyCompany", "MyApp");
        QByteArray saved_geometry = settings.value("geometry").toByteArray();
        restoreGeometry(saved_geometry);
        QByteArray saved_state = settings.value("windowState").toByteArray();
        restoreState(saved_state);
        qDebug() << "Settings loaded.";
    }
    

    These are called on application close and open, which works fine for main window position and size. I noticed though, that layout restoring doesn't cascade down to widgets contained by my MainWindows centralWidget. For instance, I tested putting a QSplitter as my centralWidget and then tried to save/restore the left and right extend of the child widget this spillter contained. Given the functions above, the application did not replicate the same extends I tried to save; instead, it repeatedly produced the same default.

    I am guessing this is due to the MainWindow only restoring the overall window geometry and state. I was hoping the function would cascade down through all child widgets and save/restore their sizes as well.

    What's the best way to implement a save state for all widgets contained by my app, so that the user can configure UI, close the application and when re-opening it will be presented the same layout he saw before shutting down?
    Is there an in-built wrapper, or will I have to manually store these settings somehow for each widget?

    EDIT: I just noticed that QSplitter offers a saveState() and restoreState() function similar to QMainWindow(). This leads me to believe it is exempt from the QMainWindow::saveState() call. Is that true? Are there other widgets behaving like this?


  • Lifetime Qt Champion

    Hi,

    For the widget directly handled by QMainWindow i.e QDockWidget, QToolBars, you have to give each widget an object name for the save/restoration to happen properly.

    As for your central widget, you'll need to implement that part yourself in your widget. It's not the role of QMainWindow to spy on the central widget inner content.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.