此文档描述如何保存和还原 窗口几何体 使用几何体特性。在 Windows,这基本上是存储结果对于 QWindow::geometry () 和调用 QWindow::setGeometry () 在下一会话先于调用 show ().
On X11, this might not work because an invisible window does not have a frame yet. The window manager will decorate the window later. When this happens, the window shifts towards the bottom/right corner of the screen depending on the size of the decoration frame. Although X provides a way to avoid this shift, some window managers fail to implement this feature.
当使用 Qt Widgets , Qt provides functions that saves and restores a widget window's geometry and state for you. QWidget::saveGeometry () saves the window geometry and maximized/fullscreen state, while QWidget::restoreGeometry () restores it. The restore function also checks if the restored geometry is outside the available screen geometry, and modifies it as appropriate if it is:
void MyMainWindow::closeEvent(QCloseEvent *event) { QSettings settings("MyCompany", "MyApp"); settings.setValue("geometry", saveGeometry()); settings.setValue("windowState", saveState()); QMainWindow::closeEvent(event); } void MainWindow::readSettings() { QSettings settings("MyCompany", "MyApp"); restoreGeometry(settings.value("myWidget/geometry").toByteArray()); restoreState(settings.value("myWidget/windowState").toByteArray()); }
另一解决方案是存储两者 pos () 和 size () 然后还原几何体使用 QWidget::resize () 和 move () 先于调用 show ().