MVC with Python

G

Georg Altmann

Hello,

I need some advice on how to implement model-view-controller. I am
trying to develop a GUI application with PyQt, yet the problem rather
applies to mvc in general, not just GUI applications.

Let's say the data is a list of objects with a common base class. The
views are either a graphical representation of the objects or some form
of textual input. The views shall change the model by using command
objects (for undo, e.g. QUndoCommand).

My current approach is to implement the model as a class with a
list-like interface, with methods insert(), remove(), __getitem__(),
__setitem__(),... and a signal to notify the views. The objects in the
list have methods to change their state as well.

My problem is, how do the commands interact with the model?
Let's say I have a command that modifies an object o in the list.

1) If list[key_to_o] returns a reference to the object, the command can
modify the object by using this reference, i.e. list[key_to_o].setX().
So there is no way for the list to know when the object changed - how
can it emit a singal then?

2) If list[key_to_o] returns a deep copy of the object,
copy_of_o = list[key_to_o], the command mofifies the copy and the
updates the list: list[key_to_o] = copy_of_o. Now the list can emit a
signal in __setitem__().
While this may work, it seems awkward to copy around objects just to
perform a possibly simple operation on them. Additionally it might not
be feasible once objects get complex.

3) The interface of the classes of the objects could be reflected in the
list class, i.e. list.set_x_on_obj(key_to_obj,x). This would probably
make the list class interface very bloated.

Of course the problem is not really limited to Python, my apologies if
I'm totally off-topic here.

Regards
Georg
 
D

David Boddie

I need some advice on how to implement model-view-controller. I am
trying to develop a GUI application with PyQt, yet the problem rather
applies to mvc in general, not just GUI applications.

Let's say the data is a list of objects with a common base class. The
views are either a graphical representation of the objects or some form
of textual input. The views shall change the model by using command
objects (for undo, e.g. QUndoCommand).
[...]

My problem is, how do the commands interact with the model?

One approach that combines the undo framework with the model/view framework
is described here:

http://doc.trolltech.com/qq/qq25-undo.html

The implementation is in C++, but it may be possible to pick up some
useful tips from the article.

David
 
G

Georg Altmann

David said:
I need some advice on how to implement model-view-controller. I am
trying to develop a GUI application with PyQt, yet the problem rather
applies to mvc in general, not just GUI applications.

Let's say the data is a list of objects with a common base class. The
views are either a graphical representation of the objects or some form
of textual input. The views shall change the model by using command
objects (for undo, e.g. QUndoCommand).
[...]

My problem is, how do the commands interact with the model?

One approach that combines the undo framework with the model/view framework
is described here:

http://doc.trolltech.com/qq/qq25-undo.html

The implementation is in C++, but it may be possible to pick up some
useful tips from the article.

Thanks for the pointer. I studied the article before, but it doesn't
seem to work for my problem:
If I understand the Qt model/view architecture correctly, data is always
based on items which are wrapped into QVariant.
(see http://doc.trolltech.com/4.4/qabstractitemmodel.html#data)

So to use the Qt model/view architecture items have to be represented as
a value. I think this leaves me with the problem of copying objects I
described before.

Regards
Georg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top