PDA

View Full Version : Conceptual MVC problem in Qt



Daniel Dekkers
10th February 2010, 15:42
Hi,

I'm new to Qt and interested in platform independence as well as following the MVC pattern to divide data, view and user control.
I am trying to set up a simple example to get a feeling of how things work.

I have the following set up:

A virtual base class c_Object with derived classes c_Circle and c_Rectangle.

A c_ObjectList class that contains a list of instances of c_Object's (either circles or rectangles). This forms the data for a...

c_QObjectListModel that inherits from QAbstractListModel. The c_ObjectList is contained as data in a has-a fashion.

A view c_QObjectListView that inherits from QListView. I've extended QListView in this class so that it catches a doubleClick event (this works fine, by "extending" the QVariant type with c_Circe and c_Rectangle and letting the model return a pointer to this item when in "Qt::EditRole" modus).

I would like to be able to create a dialog at this point to let the user edit the properties of either the circle or the rectangle he/she selected.
But now I'm stuck. I've got the pointer to c_Object at this point and would be tempted to do something like l_Object->CreateDialog().
Because CreateDialog() would be a virtual function, the correct implementation (in either c_Circle or c_Rectangle) would be called. But somehow this doesn't feel right. I would still have to add GUI code in my data classes (even if it's just a CreateDialog()). That doesn't seem to confirm to the MVC paradigm.

Anyone have another suggestion?

Kind Regards,
Daniel Dekkers
www.cthrough.nl

caduel
10th February 2010, 16:26
A fairly common way to solve that would be to introduce a type on the data. (Either by calling a type() function, or by defining a "TypeRole" that can be queried by the model.)
Then you can have a seperate createDialog() function that uses the queried type to determine the dialog that should be created. A danger is that you might forget to extend that function, when you add a new type.

HTH