[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());
        QMainWindow::closeEvent(event);
    }
    

    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();
        restoreGeometry(settings.value("geometry").toByteArray());
        if(!restoreState(settings.value("windowState").toByteArray()))
            qDebug() << "State not restored";
    }
    

    And this is the Constructor

    MyMainWindow::MyMainWindowQWidget *parent) 
    : QMainWindow(parent), _propertiesDock(nullptr), _sheetDock(nullptr), 
    _treeDock(nullptr)
    {
        ui.setupUi(this);
    
        setCentralWidget(0);
        setDockNestingEnabled(true); // For correct Docking without central widget
    
        _propertiesDock = new QDockWidget(this);
        _propertiesDock->setWindowTitle(QStringLiteral("Properties"));
        _propertiesDock->setWidget(new QWidget());
    
        _sheetDock = new QDockWidget(this);
        _sheetDock->setWindowTitle(QStringLiteral("Sheet"));
        _sheetDock->setWidget(new QWidget());
    
        _treeDock= new QDockWidget(this);
        _treeDock->setWindowTitle(QStringLiteral("Objects"));
        _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);
    
       readSettings();
    }
    

    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?

    thanks!


  • 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
 

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