Howto save state of a specific widget?
-
Hi there
I'm writing a program that it's ui contents will change frequently during runtime. I would like to save their state on exit and load last season on startup.
For example I have a QToolbar and a QComboBox on it. At runtime, some items may be added to combo box and some may be removed. Program needs to save current state of combo in a file and load it again next run. Currently it just simply saves items in a text file when exiting, and in the next run reads items again and inserts them into combo. I just wondered, is there a more advanced way to save a widget in Qt's XML format like an automatically-created .ui file? I would like to save and load state in ui files. or maybe something like restoreState of QMainWindow...
What you suggest?
-
You have to do it your self, one way could be use Qt's state machine framework
-
@vishwajeet:
How would you employ Qt State machine framework for saving the state of widgets? -
You need to find out what properties excactly you want to restore and then make these proerties persistant.
There are several persistence technologies in Qt. Choose the one which serves your needs best:QSettings is frequently used for storing window settings .
XML, SQLITE and QDataStream -> QFile can be used as well but are more heavy weight.At runtime no such thing as an .ui file is used. You need to walk through your widget tree and extract the information you want to keep for the next session. For hierarchical data XML is always a good choice. Qt makes using XML very easy by the way.
-
Andre it would not be that straight forward. In particular dynamic UI generation
Make use of state machine framework, Qt property system and any storage setting mentioned by dialingo
Lets say we derive from QAbstractState to CWidgetState::CWidgetState(QWidget *parent)
Additional API to persist state of widget on disk as part of CWidgetState class
// Loads the setting data from file, and using QMetaProperty, assign it to appropriate widget.
@CWidgetState::loadState(const QString &fileName)@// Save QMetaProperty of the widget (including child widgets) into file,
@CWidgetState::saveState(const QString &fileName)@Just to load and save property once, Qt state machine might not be required but for runtime transitions like undo/redo, this mechanism would be useful.
-
[quote author="vishwajeet" date="1298628290"]Andre it would not be that straight forward. In particular dynamic UI generation
Make use of state machine framework, Qt property system and any storage setting mentioned by dialingo
Lets say we derive from QAbstractState to CWidgetState::CWidgetState(QWidget *parent)
Additional API to persist state of widget on disk as part of CWidgetState class
// Loads the setting data from file, and using QMetaProperty, assign it to appropriate widget.
@CWidgetState::loadState(const QString &fileName)@// Save QMetaProperty of the widget (including child widgets) into file,
@CWidgetState::saveState(const QString &fileName)@Just to load and save property once, Qt state machine might not be required but for runtime transitions like undo/redo, this mechanism would be useful.
[/quote]
OK, so as I thought, the state machine API as such does not really help you at all with the persistence. I just wanted to know if I missed something there. -