PDA

View Full Version : Model - View Programming doubt.



munna
28th April 2006, 11:43
Hi,

I have an application in which data is stored in 10 tables (SQLITE) and the data is to be extracted from almost all of them and then shown in a custom view. Here are a few doubts :

1. Does having 10 tables and using all of them for view means that I will need to have 10 QAbstractTableModel (or QSqlQueryModel or QSqlTableModel.. I dont knw which one to use)?

2. Since my view is custom, how can I set the model to it ?

3. Since my view is custom, how can I update each model when user changes something in the view ? (Add row or change an item or something similar).

4. Is there a way by which I can use MVC design without using Qt's MVC framework ?

Please Help.
Thanks a lot.

wysota
28th April 2006, 12:08
1. Does having 10 tables and using all of them for view means that I will need to have 10 QAbstractTableModel (or QSqlQueryModel or QSqlTableModel.. I dont knw which one to use)?
No. It depends on the model you use. For QSqlQueryModel you can have a single model for all of the tables as you can query more than one table at the same time. For QSqlTableModel you'll need a separate model for each table. With a custom model, it depends only from you, as you implement the model.



2. Since my view is custom, how can I set the model to it ?
3. Since my view is custom, how can I update each model when user changes something in the view ? (Add row or change an item or something similar).

It depends what do you mean by "custom". If it inherits QAbstractItemView, then you can use setModel(). If it doesn't, then it's not really a view and you have to implement everything by yourself.


4. Is there a way by which I can use MVC design without using Qt's MVC framework ?
Sure, you can implement one yourself. Use signals and slots for that.

jacek
28th April 2006, 12:11
Does having 10 tables and using all of them for view means that I will need to have 10 QAbstractTableModel (or QSqlQueryModel or QSqlTableModel.. I dont knw which one to use)?
If you have one view, you need one QSqlQueryModel, QSqlRelationalTableModel or a custom model.


Since my view is custom, how can I set the model to it ?
It's still QAbstractItemView --- use setModel() method.


Since my view is custom, how can I update each model when user changes something in the view ? (Add row or change an item or something similar).
If your view was implemented properly, it should happen automagically (provided that model is editable).


Is there a way by which I can use MVC design without using Qt's MVC framework ?
Yes --- implement everything yourself.

munna
28th April 2006, 12:34
Thanks for prompt responses.



It depends what do you mean by "custom". If it inherits QAbstractItemView, then you can use setModel(). If it doesn't, then it's not really a view and you have to implement everything by yourself.


I am not sure if can subclass QAbstractItemView and that is why I say custom view.
My view is something similar to that of mac's addressbook application. The keyboard and mouse event's are also more or less similar.

Currently I do the following :

1. In my "view" class I have a reference of a class which is responsible for querying the tables and extracting the data from it. Therfore member functions of view class call member functions of the "model" (I doubt if I call it a model) class and then set the data in the view.

2. When the user is done with editing I send back the information to the "model" which updates the table.

Can you please tell me if what I do is a good design or not ? Also, can you suggest a better way to achieve it. I would be really greatful to you guys if you can suggest me some link or book which would be of some help.

Thanks a lot.

wysota
28th April 2006, 13:01
I am not sure if can subclass QAbstractItemView and that is why I say custom view.
QAbstractItemView provides methods to communicate with the model and exchange information.


My view is something similar to that of mac's addressbook application. The keyboard and mouse event's are also more or less similar.
It doesn't matter. It matters whether you use an existing view or its subclass or implement everything from scratch.


1. In my "view" class I have a reference of a class which is responsible for querying the tables and extracting the data from it. Therfore member functions of view class call member functions of the "model" (I doubt if I call it a model) class and then set the data in the view.

2. When the user is done with editing I send back the information to the "model" which updates the table.

Can you please tell me if what I do is a good design or not ?
If it works for you, then it's fine.


Also, can you suggest a better way to achieve it. I would be really greatful to you guys if you can suggest me some link or book which would be of some help.

Hard to say without knowing the structure and behaviour of your data. I would probably stick to Qt's model-view approach.

About a custom view, you can look at the Pie chart example provided with Qt or at my Chart view (http://www.wysota.eu.org/charts.html). They both provide something which we call a custom view (a subclass of QAbstractItemView).