Nominate our 2022 Qt Champions!

QDockWidget::restoreGeometry not working correctly when QMainWindow is maximized

  • I have a number of QDockWidgets, all docked in a single QMainWindow.

    I have overridden the showEvent, and after passing the event on to the base class I am restoring the dock widget's geometry

    void DockWidget::showEvent(QShowEvent* ev) 
        QByteArray byte_array = settings_.value(id + ".geometry").toByteArray();
        LOG("rest: %s", QString(byte_array.toHex()));

    In my QMainWindow::closeEvent I am calling saveSettings for each of my dock widgets

    void MainWindow::closeEvent(QCloseEvent* ev) 
        QList<DockWidget*> dock_widgets = findChildren<DockWidget*>();
        for (DockWidget* dock_widget : dock_widgets)

    In my dock widget's saveSettings function I write the result of saveGeometry to disk:

    void DockWidget::saveSettings()
        QByteArray byte_array = QDockWidget::saveGeometry();
        LOG("save: %s", QString(byte_array.toHex()));
        settings_.setValue(id + ".geometry", byte_array);

    Whilst this does work when my QMainWindow is not maximized, when it is maximized the widgets aren't restored correctly.

    In this image I have arranged my widgets just prior to closing. (linked because inline the image will be too large)

    In this image I reload my app and the widgets' geometry is incorrectly loaded.

    You can see in my functions above I log the geometry string being saved and loaded.

    I have proven to myself that the settings are correctly saved and restored again, but somehow it's not working correctly

    close the app; save the state:

    save: 01d9d0cb000200000000053f000000640000077f000001a00000053f000000640000077f000001a000000000000000000780

    open the app; restore the state: (the hex data here matches the saved one exactly)

    rest: 01d9d0cb000200000000053f000000640000077f000001a00000053f000000640000077f000001a000000000000000000780

    close the app again, having touched nothing: (the hex data is different now, as the geometry is different, see marks below)

    save: 01d9d0cb000200000000053f000000640000077f0000014e0000053f000000640000077f0000014e00000000000000000780
                                                        ^                               ^

    This doesn't happen when the window is not maximized.

    Is this a bug in Qt, or am I not using the functionality correctly?

    I am using Qt 5.5 on Ubuntu 16.04.

  • Lifetime Qt Champion


    Usually you don't do it per-widget but through QMainWindow::saveState which include dock widgets handling. The only thing to keep in mind is that you need to give objects name to all your dock widgets.

  • @SGaist thanks for the input, it is very much appreciated!

    Thanks also for the heads up on QDockWidgets not needing to use saveGeometry / restoreGeometry, I have removed that.

    Using saveGeometry / restoreGeometry on a QDockWidget obviously has no effect, as my app's behaviour is the same as it was before with the code removed.

    I am using unique objectNames - I'm using boost::uuids::uuid to generate my object name (and am also checking its unique as well, although whether that's necessary is questionable)

    std::string new_widget_id;
    	new_widget_id = to_string(boost::uuids::random_generator()());
    while (workspace->widgetIdExists(new_widget_id));

    All that said, the above change (removing saveGeometry / restoreGeometry from my dock widgets) doesn't have any effect in regards to the behaviour I'm seeing.

    I have, however, been pointed to the following 2 bugs.

    • QTBUG-16252 - QDockWidgets of maximized windows are not restored to the correct size when calling QWidget::restoreGeometry() , QMainWindow::restoreState() in a sequence
    • QTBUG-46620 - QMainWindow:restoreState doesnt restore width of QDockWidgets in maximized QMainWindow

    Rather unfortunately the 1st one, which seems to be the one I'm hitting, was first reported in 2010, so I'm not holding out hope that it's going to be resolved.

    There are, however, several possible workaround mentioned in the comments of the 2 bug reports, so I'll try those out and report back if I find a solution

  • I was able to fix the problem by using this suggestion in the comments on bug QTBUG-16252.

    The solution is to use QWidget::geometry and QWidget::setGeometry to store and load a QRect, instead of saveGeometry / restoreGeometry and the associated QByteArray

  • Lifetime Qt Champion

    Great !

    Thanks for coming back with the information !

    Since you could make it work, please mark the thread as solved so other forum users may know a solution has been found :)

  • You should care about QSizePolicy and sizeHint of your widgets inside QDockWidget. ;)