restoreGeometry() misbehaves for QMdiSubWindow

56 views Asked by At

The following program creates a QDialog as a subwindow in a MDI area.

I'm trying to use saveGeometry() and restoreGeometry() to keep the dialog at the same position and size as it was shown the previous time.

That mostly works but there is an annoying difference when the restored position is around the top of the MDI area. Meaning, it seems that restoreGeometry() is avoiding that zone.

If you run the program and select test->open, the dialog appears at (0,0). Then, if you immediately close it, and select test->open again, the dialog appears at (0,y), where y is (I suspect) the height of the title bar.

Every time you move the dialog near the top, it reincarnates a little below.

For other positions, the dialog is restored perfectly.

#include <QApplication>
#include <QtWidgets>

int main(int argc,char *argv[]) {
    QApplication app(argc,argv);
    QMainWindow  win;
    QMenu        *mnu;
    QAction      *act;
    QMdiArea     mdi;
    QByteArray   abt;
    mnu=win.menuBar()->addMenu("test");
    act=mnu->addAction("open");
    mnu->addAction(act);
    QObject::connect(
        act,
        &QAction::triggered,
        [&]() {
            if(!mdi.subWindowList().count()) {
                QDialog       dlg;
                QMdiSubWindow *sub=mdi.addSubWindow(&dlg);
                if(abt.isEmpty())
                    sub->resize(200,200);
                else
                    sub->restoreGeometry(abt);
                dlg.exec();
                abt=sub->saveGeometry();
            }
        }
    );
    win.setCentralWidget(&mdi);
    win.show();
    return app.exec();
}

Any ideas about this behavior? Thank you.

0

There are 0 answers