4

Is a model in Qt supposed to more or less fit the mold—and therefore ultimately be represented by—a list, table or tree? What if your model is a lot more complex and cannot be represented by a list, table or tree?

If this were the case, I would think that would make a model nothing but data; something comparable to an XML file or a spreadsheet.

Example: What if the model within the application in question were something more complex, like a car? I would assume the model of a car would include all sort of data and business rules about the car. There would be an engine, wheels, a frame, and many other different components that all work together to create the entire car. Each component would have its own unique set of behaviors: the frame would have a color, the engine would have a temperature, the stereo would have a volume setting, and so forth. And each component would have behaviors too: if the gas pedal is pressed, the wheels rotate and the engine heats up. Obviously, a QStringListModel or some other built-in, simplified model cannot appropriately address all of the complexities in a car.

nairware
  • 3,090
  • 9
  • 37
  • 58
  • Do you actually have a question? This is a QA site, you need to ask concrete, answerable questions. "I think I am misunderstanding" is not a question. – sashoalm Nov 18 '12 at 08:13
  • I edited my original post to be more in the form of a single, direct question. Hopefully now it is more QA-friendly. My apologies for the initial lack of focus/clarity. – nairware Nov 18 '12 at 17:14

2 Answers2

2

A model is not data but a set of callbacks. In fact, there need not be real data staying behind the model. It is more like a server to be queried.

This is like the difference between this:

int data[5] = { 0, 2, 4, 6, 8 };
void viewer(int *data, int n) {
    for (int ii = 0; ii < n; ii++)
        printf("%d, ", data[ii]);
}
int main() {
    viewer(data, 5);
}

And this:

int model(int index) {
    return index * 2;
}
typedef int (*model_function)(int);
void viewer(model_function model, int n) {
    for (int ii = 0; ii < n; ii++)
        printf("%d, ", model(ii));
}
int main() {
    viewer(model, 5);
}

Both will give you 0, 2, 4, 6, 8, but the model doesn't actually need an array to give the same values.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • So is the car object--including all of its features and behaviors--not actually part of the model/view framework? Based on your explanation, it sounds to me like the model is built off of the car, but it does not represent the car itself. It is some sort of intermediary between the actual car object (what I previously thought was the model) and the view. – nairware Nov 19 '12 at 20:29
  • You could say so. That's why the word 'model' is used - it is a model of the car. More like an interface, really. The point is, that the view queries the model, without knowing what stands behind it. The model acts like a middleman, or an interface, between the view and the actual object. That way, if the object changes, you need to change only the model, but the view can remain unchanged. So the model and the underlying object it describes are not the same. – sashoalm Nov 19 '12 at 20:40
  • In Qt, I would think the list of integers in your example could be represented using a QListItemModel. But, if we had a more complex data structure with which we were dealing (ex. a car as described above), I would assume you would be stuck essentially building your own model class, in which case you would start with QAbstractItemModel. Is that the case? Also, I would not know where to begin with the view, but I would assume that would be a similar from-scratch effort, since a list/table/tree would (likely) not suffice for visually representing a car. – nairware Nov 19 '12 at 21:33
  • It entirely depends on what aspect you want to show of the object. A car could be represented by text describing its model, engine, and relevant characteristics, which you can put in a table. It could also be represented by a 3D Studio-like simulation. You can't put that in a table. As another example, a conversation can be represented by text, an audio recording, or by a video recording. In this case you would have 3 different models of the same object. A description of an object is not the object itself. You can represent a car in a table, it just needs to be a textual representation. – sashoalm Nov 19 '12 at 22:07
  • Regarding Qt class specifics: in the example of the conversation, it sounds like you would potentially build the model for audio and/or video using QAbstractItemModel, whereas if it were to be represented visually as text, you might stick with a QListItemModel. – nairware Nov 19 '12 at 23:17
1

Try to read about MV in Qt here: similar question on SO, and of course, at such resources as Model/View Programming at http://qt-project.org/. Also, there are lot of interesting videos by VoidRealms, including this theme -- VoidRealms: C++ Qt 47 - Intro to model view programming.

Try to understand it in general, and in particular case -- how it is in Qt, and all questions and yours misunderstanding will disappear.

Community
  • 1
  • 1
NG_
  • 6,895
  • 7
  • 45
  • 67
  • I think the comment I was looking for is the second answer by @leemes in that first link: "Qt's MVC only applies to one data structure. When talking about an MVC application you should not think about QAbstractItemModel or QListView." That VoidRealms video is a good explanation of using a ListView in Qt to represent a list of strings. As I am explaining in my original question though, I am trying to figure out how to do something more complex. I am not seeing how to implement the model/view framework at the application scale, nor am I seeing how to implement complex models (ex. a car). – nairware Nov 18 '12 at 17:03