Important: Please read the Qt Code of Conduct -

saveGeometry & restoreGeometry - what am I doing wrong ? - not restoring properly

  • Hey

    I'm trying to write a template editor for widgets arangements in the app so that user can store his preferences and then restore them at later date.

    The function below essentially creates QDialog, and fills it with an existing template. Once that is done it should restore geometry arrangements/ docking of widgets/their positions. I ensured that all QObjects have unique objectName.

    The problem is that the Docking widgets arrangement is different before/after the template creation.

    Can any1 have a look at this (mostly full code - the getActiveItemTemplate/etc is not here) and let me know what you think I messed up ?

        QVector<icViewTemplate> tmpTemplate = mViewList[1]->getActiveItemTemplate();
        if (tmpTemplate[0].getIcType() == -1)return;
        QDialog *dlg = new QDialog();
        dlg->setWindowTitle("Template Configurator");
        QGridLayout *lay = new QGridLayout(dlg);
        QPushButton *btn_ok = new QPushButton("Save", dlg);
        QPushButton *btn_cancel = new QPushButton("Cancel", dlg);
        connect(btn_ok, &QPushButton::clicked, dlg, &QDialog::accept);
        connect(btn_cancel, &QPushButton::clicked, dlg, &QDialog::reject);
        QMainWindow *mw = new QMainWindow();
        lay->addWidget(mw, 0, 0, 1, 6);
        lay->addWidget(btn_ok, 1, 4, 1, 1);
        lay->addWidget(btn_cancel, 1, 5, 1, 1);
        QPair<QWidget *, QMap<QString, QWidget *>> widgetHier = buildViewFromTemplate(tmpTemplate[0]);
        mw->addDockWidget(Qt::DockWidgetArea(1), dynamic_cast<QDockWidget *>(widgetHier.first));
        if (dlg->exec() == QDialog::Accepted) {
            //qDebug() << "OldState" << tmpTemplate[0].getWidgetGeometry();
            //qDebug() << "newState" << widgetHier.first->saveGeometry();
            QStandardItem *firstItem = mViewList[1]->getFirstItem();
            firstItem->setData(dlg->saveGeometry(), Qt::UserRole + 3); // store the QByteArray in QStandardItem data QVariant.
            for (auto *widget:widgetHier.second) {
                widget->setParent(nullptr); // this should delete it all ? maybe no idea.

    Thie hierarchy of widgets is as follow. (I tried storing only the relative widget geometry and restoring that but that seems to not work sigh.)

        +mw - mainWindow
            +widgetHier.first < this is the main parent of template widgets - its a dockWidget
             // I used to save this geometry but this seems to not work so I started saving dlg geometry instead...
                    + widget.second - this is a vector of widgets that gets added to widgetHier.first as children.

    Can any1 help me out to re-parent/create properly the child Docking widgets?

  • Ok got it solved.

    Since my widgets were a little "complex" as I had QDockWidget, that had QMainWindow, so that each dock widget could house sub dock widgets. The native QT storeGeometry did not work for DockWidgets and I needed storeState/restoreState, so I subclassed my widgets to work on states of my mainWindows instead of geometries. In any case it all works now.

    Hope this helps if any1 else tries it.