views:

85

answers:

3

Hey!

I am a newcomer in the area of user interfaces. I am working now on the design of a user interface for a piece of code I have in C++ language. I've decided to use QT for creating the UI.

Now, what I am mostly concerned in is what is the proper way of separating the UI from the logic. I have some data layer, which should be accessible from different parts of my UI (different widgets, different windows). Some UI components may represent or even alter the data simultaneously.

Should I represent the data layer by a singleton and use this singleton in UI? The alternative is probably to transfer a pointer to the data to each and any UI component, which, IMHO, smells bad.

In adition, where is the best place to translate between UI-related data-structures, like QList, QVector, QString and the data structures I use in my data layer (STL + Boost). Maybe some proxy layer is required in the middle?

Can anyone recommend me a good book, article or any other source of knowledge related to my question? Any ideas or tips you can give me are also very welcomed.

Thanks.

+2  A: 

Model-View-Controller pattern is what you should rely on. http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

bad zeppelin
+1  A: 

In adition, where is the best place to translate between UI-related data-structures, like QList, QVector, QString and the data structures I use in my data layer (STL + Boost). Maybe some proxy layer is required in the middle?

Two options come to mind. You could provide functions in your data layer to return the Qt data structures, or you could wrap the Qt UI Widget classes around your own classes which take STL + Boost data structures, convert them, and pass them to the Qt Widgets.

The first option is probably easier and quicker to implement, but the second option better preserves the separation between how your data is stored and the UI.

Colin
+1  A: 

Qt provides a powerful mechanism for the Model-View pattern.

It's all documented in Qt documentation : Model/View Programming.

Basically, you'll have to implement your own Model classes, which will be the link between your logic and the UI.

Jérôme
Correct, Model/View architecture in QT is well documented. But my question is a little wider - I've a lot of windows and widgets in my application. But it seems to me that they can't be just different views for the same model. These widgets work with the same data set, but they operate on different aspects (parts) of it. For instance one widget shows the hierarchy of products to be selected, the other shows the list of products already selected by the user. So it basically means I need to implement a set of different models, right? The question is more about how to organize all these models.
Lev
AFAIK, it depends. I guess you'll have to implement a set of different models for your different views, but maybe you can use the same model if several widgets are displaying different parts of the same data. For instance, if you have a widget displaying a directory structure, and another widget displaying detailed informations about the selected file, then the same model will do. If two widgets are displaying two different data, then I guess you'll have to have two models.Also, have a look at QDataWidgetMapper.
Jérôme