tags:

views:

51

answers:

1

Hi, I am writing a GUI based application where I read a string of values from serial port every few seconds and I need to display most of the values in some type graphical indicator(I was thinking of QprogressBar maybe) that displays the range and the value. Some of the other data that I am parsing from the string are the date and fault codes. Also, the data is hierarchical.

I wanted to use the model/view architecture of Qt because I have been interested in MVC stuff for a while but have never quite wrapped my brain around how to implement it very well.

As of now, I have subclassed QAbstractItemModel and in the model I read the serial port and wrap the items parsed from the string in a Tree data structure. I can view all of the data in a QtreeView with no issues.

I have also began to subclass QAbstractItemView to build my custom view with all of the Graphical Indicators and such. This is where I am getting stuck. It seems to me that in order for me to design a view that knows how to display my custom model the view needs to know exactly how all of the data in the model is organized. Doesn't that defeat the purpose of Model/View? The QTreeView I tested the model with is basically just displaying the model as it is setup in the Tree structure but I don't want to do that because the data is not all of the same type. Is the type of data or the way you would like to present it to the user a determining factor in whether or not you should use this architecture? I always assumed it was just always better to design in an MVC style.

It seems to me like it might have been better to just subclass QWidget and then read in from the serial port and update all of subwidgets(graphical indicators, labels, etc...) from the subclass. Essentially, do everything in one class.

Does anybody understand this issue that can explain to me either what I am missing or why I shouldn't be doing it this way. As of now I am a little confused.

Thanks so much for any help!

A: 

** ** - Is this a situation where Qt Model/View architecture is not useful?
I"m going to say not necessarily - end edit

I'm not sure I fully understand your question, but let me try.

First, let's talk about MVC display pattern. This pattern is all about breaking the program into separate (and hopefully testable) sections that have their own areas of concerns.

  1. The model is data structures that describe your data. I'm sure that there is some statistical data is also present. It is important that the model not know anything about how the data is going to be displayed to the user.
  2. The View is how information is presented to user. It is not suppose to care about how the data is presented for displaying. It is important for this layer to be unaware of how the model gets the data for display.
  3. The control logic is the "glue" to connects the first two items together. This is the layer that holds all the "messy" stuff to make to good user experience. EDIT - Most of what QT calls a "view item" I would probably put in the controller layer.

But if you do this, then the model and controller layers become very testable.

With that being said, many of your points are not really related to MVC pattern. You seem to be discussing what is the optimum way to display the data. This is always a problem. And without seeing your app, I'm not really going to try to tell you what is going to look good.

But by following good MVC pattern design, you can make pretty significant revision to the display without effecting the underling code.

This being said, I'm dealing with this exact issue right now and this pattern is working well form me. If you go to codeplex.com and search for mvvm (model-view-viewmodel, term used in WPF), you will see a number of projects that use it that you can use to get more information.

If this is not enough, let me know and I may be able to give you a better answer.

photo_tom
Thanks so much. This is helpful. Let me clarify. Say I have 10 voltages, 10 temperatures, date/time, and 3 status codes that I parse from a string received on a serial port. Now, I don't want to take my data and put it in a tree, a list, table, or pie chart. Instead I want to have graphical indicators for the volts and temps, a label to display time, labels for the status codes, and labels for other calculated values. It seems like a custom view like that needs to know what exactly is coming from the model. So, I am wondering if MVC is still useful in that situation? Does that make sense?
csmithmaui
Yes MVC is still a good pattern. Because the controller layer will consolidate your readings (the model) for presentation to the view (the gauges). If you put all the complex data manipulation logic into the controller layer, then you are left with simpler model and view layers. And a controller layer that can be fully unit tested because it doesn't need to be connected to live data. It will seem like overkill when you startout. But once you start to understand it, it really makes a lot of sense.
photo_tom
Cool...thanks for your help. With your help and a lot of reading I think I am starting to understand a little better.
csmithmaui