PDA

View Full Version : How to identify data from the model in the view?



csmithmaui
16th April 2010, 11:07
Hello,
I am using Qt's Model/View Programming framework. I have subclassed QAbstractItemView and QAbstractItemModel. In my situation I need to pick out certain specific parts of the model to display in a custom view. I have used a Tree structure in the model class to represent the data. In each item of the tree, I have a column that has a text string with a unique name of my data. The model only changes internally by reading data from a serial port. In the dataChanged slot in the view I check to see which index changed by looking at the string and then update the appropriate widget in the view. I am wondering if this is the best way of doing this. It seems a little hokey to me but I don't really know how else to do it. Thanks for any help!

csmithmaui

faldzip
16th April 2010, 11:56
I think you overcomplicate things. For what you need custom view? Is there any special way in presenting your data other then tree, list or table? Normally your model should be connected somehow with this serial port data, so you update date stored in model with those new date. Then model should emit QAbstractItemModel::dataChanged() signal with affected index range as an parameter and then view will update those indices using QAbstractItemModel::data() method.
So my hint is that you do not need any special view (eventually you may need custom delegate if you want custom drawing or custom editing) but you need a model which is made properly.

csmithmaui
16th April 2010, 12:13
Hi faldzip,
Thanks for your answer. I am presenting my data other than in a tree, list or table. I am using QLabels to display some of it. I will be using Qwt to display other parts of it. The thing is, I can't just display all of the data in a standard view. I need to be able to pull out certain pieces of the data to display different ways all in the same view. The data is voltages, temperatures, date/time, current measurements. I have looked around for information on how to do this but everything I read of Qt Model/View seems to be for data that is not like mine. Maybe Model/View is not good in this case? Anyway, I know I can make it work, I was just trying to use MVC to have a more elegant solution than I usually do when I write a GUI. Any thoughts/comments you have are much appreciated.

csmithmaui

wysota
16th April 2010, 12:51
Thanks for your answer. I am presenting my data other than in a tree, list or table. I am using QLabels to display some of it. I will be using Qwt to display other parts of it. The thing is, I can't just display all of the data in a standard view. I need to be able to pull out certain pieces of the data to display different ways all in the same view.
Have you seen QDataWidgetMapper?

faldzip
16th April 2010, 12:59
So don't use Model/View, because in View you uses labels and other things which are not commonly used there, because it is not the place for QLabels. And don't use Model if you do not use its API in anyway.
My idea would be to make some class which will read all this things through the serial port with some API for exposing this data to the world in your specific way and have some signals to notify about changes, like:


class DataObject : public QObject
{
Q_OBJECT
public:
// ... c'tor and stuff
double temperature() const;
signals:
void temperatureChanged(double temp);
};


Add then create widget with all those labels, Qwt things and anything you want and make there slots for reacting for this data changes.

csmithmaui
16th April 2010, 22:51
@Wysota: No I had never heard of QDataWidgetMapper...interesting. I will check it out.

@Faldzip: Thanks a lot. This helps clarify things to me. It sounds likea good idea.

csmithmaui

wysota
16th April 2010, 23:39
@Wysota: No I had never heard of QDataWidgetMapper...interesting. I will check it out.

Please do it. It's exactly what you need instead of the custom view.

csmithmaui
17th April 2010, 00:54
@wysota:
I have been reading up on QDataWidgetMapper and it seems like it will work in my situation. I have a question though.
In the documentation it says:

Every time the current index changes, each widget is updated with data from the model via the property specified when its mapping was made.

What changes the current index? I am assuming that everytime I call setData in the model, the currentindex changes. Just wanted to verify.

Thanks for your help.

csmithmaui

wysota
17th April 2010, 08:52
What changes the current index?

You :)

QDataWidgetMapper::currentIndex