2

I want to implement the open/save functions of a GUI built with Qt and based on a QMainWindow. The whole GUI contains several QStandardItemModel, QLineEdit, QGroupBox, QSpinBox...

The first idea i have is to take all the widgets, class or model to be save and add a saveToFile / openFromFile methods, using QFile and QDataStream for data serialization.

The global architecture is a kind of implementation of the Smart UI approach however i am mixing MVC triads, and widgets inside the QMainWindow.

The GUI is not that big but i wonder if there are existing strategy to implement this open/save feature, in terms of software architecture.

I guess there are much clever ways to do so, and i am looking for any documentation or design pattern that can be used in general for this purpose.

Thanks.

Fryz
  • 2,119
  • 2
  • 25
  • 45
  • Uhh, why do you state that you want to restore the GUI?? Obviously, you want to store and restore the model, not the GUI, right? – Kuba hasn't forgotten Monica Jul 07 '16 at 14:26
  • Of course, i speak about opening a file containing the user inputs, that can be located anywhere inside models, widgets... and have its data back. The GUI itself ( widgets position, ...) is simple and not customizable. QSettings for example is not the solution here. – Fryz Jul 07 '16 at 15:40
  • Widgets should not be used to store any data. You should link the widgets to the models, and then it should be easy to serialize the model. E.g. see [this extensive answer](http://stackoverflow.com/a/31950074/1329652) or [this one that shows how to serialize a `QAbstractItemModel`](http://stackoverflow.com/a/32493783/1329652). – Kuba hasn't forgotten Monica Jul 07 '16 at 15:54
  • What you mean is building the whole GUI as a top level MVC layered architecture having each of its widgets as low level MVC triads ? And then you serialize all the models, including the top level one that contains all the low-level models ? – Fryz Jul 08 '16 at 09:25
  • 1
    It doesn't have to be so overengineered, unless your system is huge. All you really need is to tie the individual widgets to some model, using e.g. [`QDataWidgetMapper`](http://doc.qt.io/qt-5/qdatawidgetmapper.html), or a [custom widget-model binder](http://stackoverflow.com/a/11182820/1329652). From then on you can ignore the fact that there are any widgets that view the model. All of the interaction in the code will be with the data in the model. You could possibly also dynamically generate the view (its widgets, or QML items) based on a viewmodel's structure and data. – Kuba hasn't forgotten Monica Jul 08 '16 at 12:37
  • You're right about overenginering because the GUI is rather simple. I might probably consider using QDataWidgetMapper and refactor some code to do so. Thanks. – Fryz Jul 08 '16 at 13:09

0 Answers0