Skip to content

Instantly share code, notes, and snippets.

@studiofuga
Created February 2, 2016 10:42
Show Gist options
  • Save studiofuga/94c91b4678049fd42024 to your computer and use it in GitHub Desktop.
Save studiofuga/94c91b4678049fd42024 to your computer and use it in GitHub Desktop.
Qt MainWindow state and position save/restore
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// ...
QSettings settings;
restoreGeometry(settings.value("geometry").toByteArray());
restoreState(settings.value("windowState").toByteArray());
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (QMessageBox::question(this, tr("Close"), tr("Do you want to close this Window?"), QMessageBox::No, QMessageBox::Yes) == QMessageBox::No) {
event->ignore();
return;
}
QSettings settings;
settings.setValue("geometry", saveGeometry());
settings.setValue("windowState", saveState());
QMainWindow::closeEvent(event);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment