Important: Please read the Qt Code of Conduct -

[solved] Unable to restore layout changes in QDockWidgets

  • Hi,

    I am creating a MainWindow application, and when exiting the program I call

    void MyMainWindow::closeEvent(QCloseEvent *event)
        QSettings settings("MyCompany", "MyApp");
        settings.setValue("geometry", saveGeometry());
        settings.setValue("windowState", saveState());

    As it is said in the documentation.

    An in the MainWindow constructor, after adding all the dockWidgets to the MainWindow I call readSettings() :

    void Imagine::readSettings()
        QSettings settings;
        int number = settings.value("testNumberX").toInt();
            qDebug() << "State not restored";

    And this is the Constructor

    MyMainWindow::MyMainWindowQWidget *parent) 
    : QMainWindow(parent), _propertiesDock(nullptr), _sheetDock(nullptr), 
        setDockNestingEnabled(true); // For correct Docking without central widget
        _propertiesDock = new QDockWidget(this);
        _propertiesDock->setWidget(new QWidget());
        _sheetDock = new QDockWidget(this);
        _sheetDock->setWidget(new QWidget());
        _treeDock= new QDockWidget(this);
        _treeDock->setWidget(new QWidget());
        setTabPosition(Qt::TopDockWidgetArea, QTabWidget::North);
        setTabPosition(Qt::RightDockWidgetArea, QTabWidget::North);
       setTabPosition(Qt::BottomDockWidgetArea, QTabWidget::North);
       setTabPosition(Qt::LeftDockWidgetArea, QTabWidget::North);
       addDockWidget(Qt::TopDockWidgetArea, _treeDock);
       addDockWidget(Qt::TopDockWidgetArea, _sheetDock);
       addDockWidget(Qt::TopDockWidgetArea, _propertiesDock);

    The testNumberX is recovered Ok, and also the geometry, but not the state of the dockWidgets. So if I change a dockWidget from the rightArea to the leftArea, when I restart the application the changes have not been saved.

    Is it possible to change the state of the dockWidgets in an easy way?


  • Moderators

    Hi, welcome to devnet.

    saveState() uses objects names. You are not giving your dock widgets names so they can't be identified. Use setObjectName() on the docs (and toolbars too if you have any movable) before saving/restoring.

  • Thanks Chris, that solved the problem :)

Log in to reply