PDA

View Full Version : Application design using MVC



markd_mms
3rd May 2007, 00:35
This is a hypothetical although it could apply to any number of programs I'm working on at the moment.

Say my program is based around displaying and editing data in a database and I use the active record pattern for classes representing the data in the database. Single entities are accessed via a list for which I would use a QSqlTableModel and QTableView to display.

My question is, to keep with the pattern should I use QSqlTableModel directly or should I subclass QAbstractItem/ListModel and implement say a CompanyListModel (if I used it to list companies from the database) which would internally use a QSqlQuery to instantiate a list of Company objects? Would this be considered good design or is it just overkill?

TIA

wysota
3rd May 2007, 15:41
IMO you just need to adjust the delegate to return an editor like a combobox that allows you to choose from a list of options.

markd_mms
5th May 2007, 03:39
Thanks but I don't think that was the answer I was looking for. I'll try and explain myself a bit better.

We have a program at work that displays companies we deal with, their addresses and contacts, which are stored in a database. At the moment it's design doesn't follow a pattern - when inserting or editing data the additton or changes are made using a query that is instantiated in the onclick event of an ok button.

Inserting or editing data can easily be changed to use the active record pattern where the properties and functions to load and save a company, address or contact are encapsulated in their own classes. This would mean that in the onclick event of the ok button we can now instantiate a Company and call Company::save() to save it to the database.

My question is, in keeping with good design and using patterns, when listing all companies should I use a QSqlQueryModel which selects * from company and displays it in a QTableView, or should I subclass QAbstractItem/ListModel in such a way so that internally it keeps a list of Company objects which it has loaded from the database?

I hope that's a bit clearer.

TIA
Mark

wysota
5th May 2007, 08:32
You should use QSqlTableModel or QSqlRelationalTableModel and QDataWidgetMapper. If you can't have a table (or a database view) that carries all the data you need, then either implement a proxy model or a classic model based on QSqlQueryModel (implement write capabilities).