473,769 Members | 5,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: MVC with Python

Le Tuesday 16 September 2008 14:47:02 Marco Bizzarri, vous avez écrit*:
On Tue, Sep 16, 2008 at 1:26 PM, Georg Altmann <ge****@georg e-net.dewrote:
Marco Bizzarri schrieb:
On Mon, Sep 15, 2008 at 9:37 PM, Georg Altmann <ge****@georg e-net.de>

wrote:
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 emita
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_o bj(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

Well, I think one of the assumptions of the MVC is that the view can
be notified about the changes in the model. According to your
interface, the View can just be notified on operations about the
*whole* model, like adding or removing elements from it.

But the elements of your list-like class should be a part of your
model, and therefore should be able to notify the Views by
themselfves.
Ok, consider this: say the model needs to compute some sort of value by
iterating over all objects in the list (for example some sort of hash).
Some kind of view then displays this value.
I could implement this by connecting a signal in each object to a slot in
the list class which in turn emits a signal which is connected to the
view.

But this implies all the objects in the list have to be instances of
QObject. I wonder if this is isn't a weird design, because for example
all value type classes in Qt don't derive from QObject, e.g. QString. My
list objects are not that simple (in terms of the data structure). So is
it ok to make them QObjects?

Did you take a look at this?

http://www.potu.com/man/doc.trolltec...ogramming.html

It seems to imply that you should use QtAbstractItemM odel as a base
class for all your models. But since I'm not an expert about Qt, I
cannot really say.

Regards
Marco
Regards
Georg

--
http://mail.python.org/mailman/listinfo/python-list
It is not about QT, it is about MVC. In MVC, code which implement the model
should be completely ignorant of the libraries used for gui, and the gui part
of the application shouldn't acces directly to your model logic. This why
there is a third layer which abstract the logic of your datas and provide
standard operation for the gui code. This third layer (Controllers), should
be as few as possible coupled to the specificities of the gui library (but
must be).

The quoted papers explain rather well how to do this with QT (apart they call
controllers "delegates" ).

For medium to big project, don't be afraid to divide your program in three
packages, one for the model, in which you must not have any reference to QT
API of some sort, one for the controllers, where you provide events and
methods to manipulate your model, one for gui, where you just build windows,
subscribe to controller's events and use their functions as callbacks for
your widgets.

--
_____________

Maric Michaud
Sep 16 '08 #1
0 1322

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.