When programming a GUI (usually with Python and PyQt4), I try to organise my code neatly and I often face the same question:
How should the invisible objects (data) of my code relate to their visible widget counterparts (graphic representation)?
For instance, suppose I have a list of entries from a linguistic dictionary. Each entry is an abstract data object with various attributes and functions. Each entry also is represented graphically as a widget the user can interact with.
I believe there are four possible ways of organising them:
- the widget object is a child of the data object;
- the data object is a child of the widget object;
- the widget object and the data item are merged into one object (data are attributes of the widget);
- the widget object and the data object exist separately and the latter is fed into the former (e.g. as *args)
Now I am not sure whether or not there is some conventional agreement regarding this and I am not certain either whether these possible arrangements really make a difference.
What is the best practice?
I’ve never used Python or QT, but it sounds like you’re looking for an organization called:
Model-View-Controller
The idea is, that your Data Object (Model) can live far away from your Widget (View). They are coordinated by a Controller.
This is extremely prevalent in Objective-C / Cocoa, and to a lesser extent, Android.
Most modern web frameworks are also based on this concept, as well.
EDIT: At least to some degree, QT does as well:
http://doc.qt.nokia.com/qq/qq10-mvc.html