Results 1 to 4 of 4

Thread: [QT4] Saving a widget state

  1. #1
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default [QT4] Saving a widget state

    Is there a way to save a widget's state (size, positions and sizes of child widgets, etc) similar to the way that a QMainWindow stores its toolbars and actual size?

    Because my app allows the user to re-size the layout (using horizontal and vertical separators), it will likely be desirable to save any changes to the layout so that they can be applied on the next run.
    Life without passion is death in disguise

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QT4] Saving a widget state

    You can store the geometry of a widget into QSettings object.

  3. #3
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: [QT4] Saving a widget state

    Ok... is there a way to iterate through all widgets in a widget so I can store the internal geometry? It's easy for me to store my main widget's geometry by just using the geometry() and setGeometry() functions in conjunction with QSettings... but I see no easy way to do so with internal widgets. This means that if one widget's separator was moved to make one smaller and one larger, this is not preserved the next time the app is run.

    I had thought that I could run through the below code to make it work, but it's behaving strangely as I do so (taken from my main window's code):
    Qt Code:
    1. QLayout * myLayout = layout();
    2. for(int index(0); index < myLayout->count(); index++)
    3. {
    4. QWidget * child = myLayout->itemAt(index)->widget();
    5. if(child)
    6. { //Then it must really be a widget and not a layout
    7. QSettings settings;
    8. settings.beginGroup(QString("geometry/") + QString::number(index));
    9. if(!settings.value("geometry").isNull())
    10. {
    11. child->setGeometry(settings.value("geometry").toRect());
    12. }
    13. settings.endGroup();
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Simple-looking stuff... but it seg-faults on startup (specifically, on the 3rd run of line 4)

    It prints:
    QMainWindowLayout::count()
    QMainWindowLayout::count()
    QMainWindowLayout::count()
    Segmentation fault
    I'm kinda at a loss at this point
    Last edited by KShots; 28th April 2006 at 20:12.
    Life without passion is death in disguise

  4. #4
    Join Date
    Jan 2006
    Location
    Cambridge, MA
    Posts
    32
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [QT4] Saving a widget state

    I'd guess that for some reason itemAt() is returning NULL. However, for a more straight forward way of getting at childern, have a look at QObject::childern() for a list of child objects regardless of layout nesting.

    For a quick way to save "Qt Properties" to your text format of choice have a look at:

    QVariant property ( const char * name )
    bool setProperty ( const char * name, const QVariant & value )

    --Justin Noel
    justin@ics.com

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  2. Storing widget state
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 10th April 2008, 09:55
  3. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  4. Saving state of connections
    By Chuk in forum Qt Programming
    Replies: 11
    Last Post: 26th August 2007, 17:01
  5. [Qt4] - Moving Widget...
    By IPFreely in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2006, 09:32

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.