[Qt-interest] Saving the state of all your widgets.

Pascal Patry iscy at invalidip.com
Wed Mar 4 19:19:50 CET 2009


On March 4, 2009 01:03:34 pm Knapp wrote:
> Hello, I have a program with a bunch of spinboxes and lineEdits etc. I
> want to have the person click new or save and have the state of all of
> them reset, loaded or saved. Is there and easy way to do this or must
> I write a method to grab the data from each one, turn it into a var
> and then save it?
>
> I was thinking maybe I should make each widget receive a signal and
> then transmit that data and have a slot gather it all up. Maybe that
> is to crazy though.
>
> Thanks for any ideas.

There are no easy and simple ways already implemented, however you can do it pretty 
easily.

By using QObject::children(), you can go through all the QWidgets of the application. You 
may need to do it recursively, but that's not really complicated.

Once you have the complete list, you can use QObject::inherits() to know if the type 
interests you. If it does, then you can get the needed data through QObject::property().

Other than this implementation, you should consider having a struct/list that describes 
all the interested properties for the "class name". For instance:

"QSpinBox" -> "value"
"QLineEdit" -> "text"
...

Since you are sure that all these properties are already stored as QVariant, you don't 
actually need to know the internals of the widgets, just the property name holding the 
needed data. Once you gather everything, you can easily save this using the name of the 
widget (QObject::objectName()) with the data.





More information about the Qt-interest-old mailing list