PDA

View Full Version : saveState() problem



waynew
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");
query.addBindValue(ba);
query.exec();

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");
query.last();
QByteArray ba = query.value(0).toByteArray();

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

restoreState(ba);

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


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

sepp
1st March 2011, 15:08
Hi,

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...

d_stranz
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.

mcosta
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.