Qt Save application state
-
Hello,
I have a qt widgets applications and i have a lot of widgets and csv models which the user can modify. I want to store the state of the application meaning properties of various widgets(there are literally too many widgets) and also tableview models. How and where do i store this. If i need to store in a file, where should the file be located such that i dont have to change the path of the application in different OSs ?
Thanks for any help in advance -
@sachinrd Hi,
as for "where" that is rather simple question, as Qt provides transparent and cross platform approach to this via QSettings class.As for "how" - that all depends. Some examples are provided in the QSettings documentation, some others are in respective widgets documentation. You should look for
saveState()
andsaveGeometry()
methods.Please bear in mind that saving geometry is a tricky thing - Qt computes those for widgets based on layouts applied, so you should rather save the size of the main window only. As for states, that's different thing and would rather require you to carefully think over what you exactly want to restore afterwards, what can be restored by hand and what should not due to said automatisation.
-
Hi @artwaw . Thanks for the quick reply.
I understand i can store widget states in QSettings. What about csv data which is present as a model in the application. i cant store model data in QSettings right ?
And also I have multiple graphicsview scenes and multiple widgets in the scene. Wouldnt it be better to store all the widget properties as a json to a file, where i get to the freedom to have a proper structure ? And i dont see a save state method in most of the widgets/graphicsitems that i am using . -
@sachinrd It is your decision to make.
QSettings, if you read the documentation, is not exactly designed to store such data as CSV sets. Technically there is nothing to stop you from doing that though. You can also switch from platform dependent location (registry, plist, etc) to INI format and save it wherever.
If you think JSON then of course you'll have to write your own system of storage. Qt has a few classes that might help you to construct such objects.
Or you can take a look at QDataStream and an overview of data serialisation in Qt. -
@sachinrd said in Qt Save application state:
And i dont see a save state method in most of the widgets/graphicsitems that i am using .
There isn't. Don't try to serialize actual widgets. You need to store the changed attributes via some kind of model. And use that to reconstruct the widgets when required.
You can save that kind of state by whatever suits you, e.g. settings, JSON.