views:

1280

answers:

2

Greetings,

I am not a professional application developer so am probably not as familiar with the model/view design pattern as I should be. Nonetheless, I am trying to use it in a hobby app and failing miserably. The primary problem I am having is that the data I am trying to display and modify is not easily represented with an extension of QAbstractItemModel.

The data I am trying to encapsulate is essentially a growable/shrinkable, mutable list of integers. Should I abandon the model/view pattern for data like this? It seems more appropriate when the "dimensions" of the data are fixed. If not, is there an example of an implementation that I could take a gander at, or a good book that I should pick up?

Regards.

A: 

QAbstractItemModel is just one, admittedly very limited way of implementing the Model/View design pattern. If you see that your situation doesn't fit it neatly don't bother working too hard to force it.

A better approach for you would probably be to just pull your own Model class with your own View classes and abandon QAbstractItemModel. There is more to this design pattern than the weird flavor implemented in QT and that flavour only works well for very particular applications.

I suggest you read about it some more and design your own Model-View setup. Your class design is very likely to be cleaner and better understood if you pull your own.

shoosh
I think you have to implement QAbstractItemModel if you want to use your model in standard views (tree, table, etc)
Eugene
I've considered this, but the real skill I'm exercising with this project is working within a framework (which I do rarely in my "real" job).
Dan O
Deriving from and implementing QAbstractItemModel (or one of its derived classes) is how you work within (Qt's) framework :).
Eugene
+2  A: 

I would look at QAbstractListModel. It sounds like a more relevant model than the basic QAbstractItemModel.

There is also a rather different view of that model in the example: Puzzle

If you need a higher level look at Model/View, check out this.

Adam W
The puzzle example is interesting, but a little odd. Do you happen to understand why the author didn't use the model view pattern for the puzzle widget?
Dan O
So I ended up not going with the modelview/pattern. I will do as the author of puzzle did and only implement a view class if I want to make use of the existing QT model classes.My original plan had been to create a custom model as well. No custom model means no custom view, at least for now.
Dan O
Right, only do what is needed. Don't try to make your model work with a Qt view if it doesn't. I believe the puzzle example doesn't use Model/View since the QAbstractItemView class really doesn't fit the design.
Adam W