My goal is to create a thick client to the database. Basically it is all about managing three lists of data.
I would like to slice my application into decoupled layers so using Qt's Model/View framework seems natural to me.
- When should I create QSql*Model instances?
I need to be able to connect/disconnect to/from the database several times (I have menu items for that). I'm not happy with deleting a bunch of models and creating them once again upon every connect/disconnect.
Is there any alternative approach?
- Where should I create QSql*Model instances?
I don't think MainWindow or any other GUI-related class is supposed to hold the code like this:
m_goodsModel->setRelation(1, QSqlRelation("Level", "LevelId", "Name"));
I want to decouple the GUI from the data structure. Any ideas how to do that?
- When and where should I bind my views to models?
I need to represent my three lists in a dozen of ways. If I recreate the models every time I connect/disconnect I will need to inject newly created models into all the views again.
It would be great if I could do that only once, but I have no clue how.
- What to do with the nasty
QSqlTableModel::select()
method?
This one drives me crazy. In contrast to other models (e.g. QStringListModel
, QFileSystemModel
, etc) where the data is ready to use out of the box, models derived from QSqlTableModel
are useless until you manually invoke their select()
method. Before that invocation the model is empty so as the views using that model; the header data is not populated either, so the view doesn't even know what columns does it have to render.
As I cannot avoid select()
invocation I wonder where should I put it so that it fits nice? I don't think MainWindow or any other GUI-related classes should contain that code.
- Performance and robustness
I'm not happy to reinitialize everything upon db reconnection. It takes too long to do that (I mean during execution). I also would like to avoid crashes during model recreation process as the views may still refer to them.
Isn't there any other way to set everything just once and handle reconnection gracefully?