What are elements of dramatic representation?

What level of separation should my UI elements and model objects have?

  • I'm building a desktop app in QT (although that may be irrelevant) and I'm having a hard time working through the class structure and layout. The data model is fairly simple with a root container with a number of containers of items. Consider the following representation: Root + Parent ++ Child The UI is fairly simple and follows that data model with a main window with a scrollable area (root), widgets that contain layouts (parents) of custom widgets (children) with some labels and buttons. My trouble is with handling events that need to go up the chain and then back down like moving a child from one parent to another, moving elements, or updating child meta-data that impacts several to many other widgets. I'm currently splitting UI widgets and model objects but having each widget and corresponding model object pointing to and from each other feels cumbersome and I think it is leading to too much maintenance.

  • Answer:

    I am not familiar with QT, but is there a way to delegate the events to the parents/root? Your problem is due to the fact that each child handles its own events and maintains relationship with the corresponding model object. By delegating the events to the top level objects (root is OK if the events are sequenced and the user is doing one thing at a time. If this is not acceptable, first level parent objects) event handling and hence the need to maintain the relationships will be limited to a few objects. The event needs to contain enough information on the event source in order for this to work. Read up on event delegation in the browser to get an idea on how this is done in web apps. I am sure it is possible to replicate this behavior in the desktop app as well.

Raghavendra Kidiyoor at Quora Visit the source

Was this solution helpful to you?

Other answers

One way to delegate events from one qobject to another is to declare a signal in your child class and connect it to the event signal you might try to handle otherwise (usually the connection is established in the constructor).  Then you connect a slot of your root's to the signal. This being said, you may try reorganizing your code instead of centralizing control.  For example, you could have a single model that all your children have access to that is owned by the root.  Each child would update the appropriate part of the model as needed and the root could respond to change notifications in the model. That way, instead of relying on event propagation, you would just be subscribed to model changes which would be far more flexible (e.g you could write unit tests of the model without having to create any root or children).

Vitali Lovich

Try using a notification model, where widgets (or their controllers) can subscribe to event notifications.

Jarin Udom

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.