#include <QCloseEvent>
#include <QShowEvent>
void MainWindow::showEvent(QShowEvent * event) {
restoreGeometry(_config->IniRead(QStringLiteral("MainWindow/geometry")).toByteArray()); // 读取位置
restoreState(_config->IniRead(QStringLiteral("MainWindow/state")).toByteArray()); // 读取状态
}
void MainWindow::closeEvent(QCloseEvent * event) {
_config->IniWrite(QStringLiteral("MainWindow/geometry"), saveGeometry()); // 记住位置
_config->IniWrite(QStringLiteral("MainWindow/state"), saveState()); // 记住状态
}
void MainWindow::showEvent(QShowEvent * event) {
restoreGeometry(_config->IniRead(QStringLiteral("MainWindow/geometry")).toByteArray()); // 读取位置
restoreState(_config->IniRead(QStringLiteral("MainWindow/state")).toByteArray()); // 读取状态
}
void MainWindow::closeEvent(QCloseEvent * event) {
_config->IniWrite(QStringLiteral("MainWindow/geometry"), saveGeometry()); // 记住位置
_config->IniWrite(QStringLiteral("MainWindow/state"), saveState()); // 记住状态
}
=====================
官方手册
bool QWidget::restoreGeometry(const QByteArray &geometry)
Restores the geometry and state of top-level widgets stored in the byte array geometry. Returns true on success; otherwise returns false.
If the restored geometry is off-screen, it will be modified to be inside the available screen geometry.
To restore geometry saved using QSettings, you can use code like this:
QSettings settings("MyCompany", "MyApp"); myWidget->restoreGeometry(settings.value("myWidget/geometry").toByteArray());
See the Window Geometry documentation for an overview of geometry issues with windows.
Use QMainWindow::restoreState() to restore the geometry and the state of toolbars and dock widgets.
This function was introduced in Qt 4.2.
See also saveGeometry(), QSettings, QMainWindow::saveState(), and QMainWindow::restoreState().
bool QMainWindow::restoreState(const QByteArray &state, int version = 0)
Restores the state of this mainwindow's toolbars and dockwidgets. Also restores the corner settings too. The version number is compared with that stored in state. If they do not match, the mainwindow's state is left unchanged, and this function returns false; otherwise, the state is restored, and this function returns true.
To restore geometry saved using QSettings, you can use code like this:
void MainWindow::readSettings()
{
QSettings settings("MyCompany", "MyApp");
restoreGeometry(settings.value("myWidget/geometry").toByteArray());
restoreState(settings.value("myWidget/windowState").toByteArray());
}
See also saveState(), QWidget::saveGeometry(), QWidget::restoreGeometry(), and restoreDockWidget().