Results 1 to 2 of 2

Thread: Conceptual MVC problem in Qt

  1. #1
    Join Date
    Feb 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Conceptual MVC problem in Qt

    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

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Conceptual MVC problem in Qt

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.