Writing to/reading from disk via QDataStream()
-
Hello,
This is my first post, and I tried searching for something similar but to no avail.
I started developing a GUI app with a fairly straightforward structure. The main workspace consists of QWidget objects (each with their own set of basic widgets on them, like line edits, comboboxes, etc.) embedded on QTabWidget tabs. Navigation is supplemented by a QTreeWidget object that acts as a tab browser/explorer. The signals and slots are connected in a way that clicking on any item in the tree widget will activate the item's corresponding tab. To achieve this, I set each tab's widget as each corresponding tree widget item's data by using the QTreeWidgetItem class's setData method.
There's more to it, of course, but that's the crux of it. I'm seeking advice with respect to writing to and reading from disk. Although I'm toying with other methods, my initial inclination is to save in binary format via a QDataStream object. Unfortunately, it seems that objects subclassed directly from QWidget can't themselves be written to the stream. So, prior to writing each tree widget item, I need to write to the stream each QWidget object's component widgets' data (line edits, comboboxes, etc.), then reset the tree item's data to strip out the QWidget object, and finally write the tree widget item itself. Of course, when I then read that data in, I've got a mass of unstructured data, and to re-structure (and, in essence, re-create) the file I more or less need to call my own custom tree-item/tab pair creation methods (which are used for creating tree-item/tab pairs outside of just reading/writing to disk) on each tree item that is read in.
I'm a novice developer, so I just want to ask flat-out -- am I approaching this the right way? (I mean, I gave it a few cursory tests before going to bed last night, and it works, but it feels like a workaround hack, and I strive for something better than merely functioning, and since I was just toying around with this yesterday I haven't yet gotten the chance to see how long the read/write operations would take on a file heftier than a mere 20k.) Note that I'm using PySide, which is why I posted in this sub-forum, although I don't think that much matters for this issue.
-
Hi and welcome to devnet,
Indeed, QObject can't be serialize like that. Since you have complex UI what you could use is XML. Dump the state of your widgets with an identifier that will allow to retrieve the correct widget data when loading your file. Make each widget dump/load itself so if you add a new widget later you would only need to write these two functions.
Hope it helps
-
Require ? No, a simple parser using QXmlStreamReader can be enough.
-
SGaist, thank you for the helpful reply. Someone I spoke with about the issue suggested that I should separate my model/view better in order to make serialization a much more straightforward task -- I had previously been using the QTreeWidget class rather than a QTreeView and linked QStandardItemModel. I am in the midst of re-writing sections of my code to fully segregate my data and view. (I am struggling with what appears to be a bug where the setData() method of the QComboBox() class will NOT take as a value QStandardItems, but I plan to create a new thread for that.)
Nonetheless, I believe XML is very well-suited for serializing the data as its structure, like trees, is naturally hierarchical. I thank you again for your answer.
-
Very good suggestion, less dependencies is better.
QComboBox doesn't have a setData function, did you mean setItemData ? When using QComboBox you don't have to handle the item themselves only their content