Qt Restoring Geometry Example

mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H
 
#include <QWidget>
#include <QCloseEvent>
#include <QSettings>
 
class MyWidget : public QWidget
{
    Q_OBJECT
 
public:
    explicit MyWidget(QWidget *parent = 0);
    ~MyWidget();
 
protected:
    void closeEvent(QCloseEvent *event);
 
private:
    void readSettings();
    void writeSettings();
};
 
#endif // MYWIDGET_H

mywidget.cpp

#include "mywidget.h"
#include "ui_mywidget.h"
 
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
    readSettings();
}
 
MyWidget::~MyWidget()
{
 
}
 
void MyWidget::readSettings()
{
    QSettings settings("Programming Examples", "Geometry Example");
    QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
    QSize size = settings.value("size", QSize(400, 400)).toSize();
    resize(size);
    move(pos);
}
 
void MyWidget::writeSettings()
{
    QSettings settings("Programming Examples", "Geometry Example");
    settings.setValue("pos", pos());
    settings.setValue("size", size());
}
 
void MyWidget::closeEvent(QCloseEvent *event)
{
 
    writeSettings();
    event->accept();
 
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.