Results 1 to 4 of 4

Thread: saveState() problem

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default saveState() problem

    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.

    Qt Code:
    1. // saving the state - this is called in the closeEvent:
    2. void MainWindow::saveSettings() {
    3. ba = saveState();
    4.  
    5. qDebug() << "saving ba size is " << ba.size();
    6.  
    7. QSqlDatabase db = QSqlDatabase::database(ctrlConn);
    8. QSqlQuery query(db);
    9. query.prepare("UPDATE settings set state=? where id=1");
    10. query.addBindValue(ba);
    11. query.exec();
    12.  
    13. qDebug() << "saving state " << query.lastError();
    14. }
    To copy to clipboard, switch view to plain text mode 
    saving ba size with debug shows 93

    Now restoring the state called from the constructor.
    Qt Code:
    1. void MainWindow::restoreSettings() {
    2. QSqlDatabase db = QSqlDatabase::database(ctrlConn);
    3. QSqlQuery query(db);
    4. query.exec("SELECT state from settings where id = 1");
    5. query.last();
    6. QByteArray ba = query.value(0).toByteArray();
    7.  
    8. qDebug() << "restoring ba size is " << ba.size();
    9.  
    10. restoreState(ba);
    11.  
    12. qDebug() << "restoring state " << query.lastError();
    13. }
    To copy to clipboard, switch view to plain text mode 

    debug shows restore state ba size is 93.
    What's wrong here?
    Last edited by waynew; 29th January 2010 at 01:29.

  2. #2
    Join Date
    Feb 2006
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: saveState() problem

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

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: saveState() problem

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: saveState() problem

    saveGeometry is more complete because it saves also flag as minimized, maximized, geometry before maximization, ...
    in new code use it.
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. QDockWidget saveState and restoreState
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 28th January 2016, 09:56
  2. QHeaderView saveState()
    By waynew in forum Qt Programming
    Replies: 2
    Last Post: 26th December 2009, 15:46
  3. saveState with multiple windows
    By chezifresh in forum Qt Programming
    Replies: 1
    Last Post: 29th September 2009, 09:16
  4. saveState()
    By coderbob in forum Qt Programming
    Replies: 2
    Last Post: 27th March 2008, 11:50
  5. 4.2 saveState
    By merlvingian in forum Qt Programming
    Replies: 6
    Last Post: 1st November 2007, 15:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.