View Full Version : saveState() problem

29th January 2010, 02:17
Can't seem to get this to work. I'm trying to save the mainwindow state to a database and restore it on startup so the window size and position will be the same as when the application was quit. The problem is that the size and positions don't get restored.

// saving the state - this is called in the closeEvent:
void MainWindow::saveSettings() {
QByteArray ba;
ba = saveState();

qDebug() << "saving ba size is " << ba.size();

QSqlDatabase db = QSqlDatabase::database(ctrlConn);
QSqlQuery query(db);
query.prepare("UPDATE settings set state=? where id=1");

qDebug() << "saving state " << query.lastError();

saving ba size with debug shows 93

Now restoring the state called from the constructor.

void MainWindow::restoreSettings() {
QSqlDatabase db = QSqlDatabase::database(ctrlConn);
QSqlQuery query(db);
query.exec("SELECT state from settings where id = 1");
QByteArray ba = query.value(0).toByteArray();

qDebug() << "restoring ba size is " << ba.size();


qDebug() << "restoring state " << query.lastError();

debug shows restore state ba size is 93.
What's wrong here?

1st March 2011, 15:08

as far as i know from the docs QMainWindow::saveState()/restoreState() save/restore the current state of this mainwindow's toolbars and dockwidgets. To save/restore the size and position you have to use QWidget::saveGeometry()/restoreGeometry(). Hope this helps...

1st March 2011, 17:42
I use size() / resize() and pos() / move() to save and restore QMainWindow size and position, but I suppose saveGeometry() / rstoreGeometry() would do the same thing.

1st March 2011, 17:49
saveGeometry is more complete because it saves also flag as minimized, maximized, geometry before maximization, ...
in new code use it.