QDockWidget::restoreGeometry not working correctly when QMainWindow is maximized

北城余情 提交于 2019-12-05 18:19:48

This is a bug in Qt. Specifically, QTBUG-46620 and possibly also QTBUG-16252.

The bug report for QTBUG-46620 details a work-around, which you should try. To begin with, ensure that the main window geometry and main window state are saved when you close the application (note that you should not have to save the geometry of each dock window separately):

void MainWindow::closeEvent(QCloseEvent* ev)
{
     settings_.setValue("geometry", saveGeometry());
     settings_.setValue("state", saveState());
}

Then, restore the geometry as follows:

restoreGeometry(settings.value("geometry").toByteArray());
if (isMaximized())
{
    setGeometry( QApplication::desktop()->availableGeometry(this) );
}
restoreState(settings.value("windowState").toByteArray());

If the you have trouble with the above work-around, you may also have to save the maximized state of the window:

void MainWindow::closeEvent(QCloseEvent* ev)
{
     settings_.setValue("geometry", saveGeometry());
     settings_.setValue("state", saveState());
     settings_.setValue("maximized", isMaximized());
}

Then restore as follows:

restoreGeometry(settings.value("geometry").toByteArray());
if (settings.value("maximized").toBool())
{
    showMaximized();
    setGeometry( QApplication::desktop()->availableGeometry(this) );
}
restoreState(settings.value("windowState").toByteArray());

Note that these work-arounds may cause some warning messages to be generated on some platforms.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!