PDA

View Full Version : [QT4] Saving a widget state



KShots
26th April 2006, 04:21
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.

wysota
26th April 2006, 06:48
You can store the geometry of a widget into QSettings object.

KShots
28th April 2006, 19:56
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):
QLayout * myLayout = layout();
for(int index(0); index < myLayout->count(); index++)
{
QWidget * child = myLayout->itemAt(index)->widget();
if(child)
{ //Then it must really be a widget and not a layout
QSettings settings;
settings.beginGroup(QString("geometry/") + QString::number(index));
if(!settings.value("geometry").isNull())
{
child->setGeometry(settings.value("geometry").toRect());
}
settings.endGroup();
}
}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 faultI'm kinda at a loss at this point :(

Glitch
1st May 2006, 14:31
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