Save and restore Qtreewidget
-
HI,
How can I do this?
The idea is to save the entire content of Qtreewidget to a file and load it. Preferably in some native QT format to do it quickly and safely.
I can do it from Python itself, but maybe there are some simpler mechanisms built into PyQT? -
Hi,
There's no premade model serialization so you have to implement it yourself.
You could use the QDataStream class for that but depending on the content of your QTreeWidget, generating a JSON data structure might be enough. You could store the resulting string using QSettings along other parameters of your application.
But first things first, what do you want to save and why ?
-
Hi,
There's no premade model serialization so you have to implement it yourself.
You could use the QDataStream class for that but depending on the content of your QTreeWidget, generating a JSON data structure might be enough. You could store the resulting string using QSettings along other parameters of your application.
But first things first, what do you want to save and why ?
@SGaist My program scans the contents of the database of the graphics engine used in television. These databases are quite large, so the entire procedure of scanning them and arranging them in the right way in Qtreewidget takes a lot of time. However, these databases rarely change. I would like to be able to save the contents of Qtreewidget after scanning so that I can open it and work with it without having to scan the engine every time.
-
@SGaist My program scans the contents of the database of the graphics engine used in television. These databases are quite large, so the entire procedure of scanning them and arranging them in the right way in Qtreewidget takes a lot of time. However, these databases rarely change. I would like to be able to save the contents of Qtreewidget after scanning so that I can open it and work with it without having to scan the engine every time.
@MarcinOS
AQTreeWidgetis aQTreeViewwith an internal model attached to it. You cannot serialize aQTreeViewbecause it is aQWidget/QObject, and those cannot be serialized. In principle you should just be able to serialize the model part, and use that to reconstruct it in a newQTreeWidgetwhen reading back in.If
QTreeWidgetsaves all its customizable data in the model via roles you can usemodel.data()to read and save the data in all the roles andmodel.setData()to restore it. Or, you can do that for the individualQTreeWidgetItems, I notice that they have QDataStream & operator<<(QDataStream &out, const QTreeWidgetItem &item) (have to check how you do this from Python/PySide, might need to call https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QTreeWidgetItem.html#PySide6.QtWidgets.PySide6.QtWidgets.QTreeWidgetItem.write directly) already written for serializing them to/from aQDataStream. You may also need to save/restore the PySide6.QtWidgets.QTreeWidget.headerItem(). -
@SGaist My program scans the contents of the database of the graphics engine used in television. These databases are quite large, so the entire procedure of scanning them and arranging them in the right way in Qtreewidget takes a lot of time. However, these databases rarely change. I would like to be able to save the contents of Qtreewidget after scanning so that I can open it and work with it without having to scan the engine every time.
@MarcinOS in that case, I would rather recommend that you get the database, apply the processing you need to have it in a standard known form and then use that form with your application. That way you stay independent of whatever widget you will use to show the data.