PDA

View Full Version : Questions about Model-View Architecture in QT



Polnareff
2nd June 2010, 20:18
Hi,

So I've been reading a lot about the Model-View-Delegate architecture that QT employs. I've noticed that this is used primarily with widgets like the ListView, TreeView, and TableView and ColumnView. In my own code, I'd like to be able to have a Model for other widgets, like QTexEdit or QSpinBox. It seems, though, that this is only possible if I create a widget which inherits from something like QAbstractItemView. While there is tons of documentation on creating your own models and delegates, there doesn't seem to be much for views, and it seems like a lot more work.

I read about Delegates here (http://doc.trolltech.com/4.5/itemviews-spinboxdelegate.html), and as far as I can make out, it'll allow for attaching a data model to a widget as I described, but the delegate itself must still be a delegate of a view, of course, and so the problem persists.

What exactly is the correct thing to do here, and is there perhaps any documentation? Thanks :)

Coises
2nd June 2010, 23:28
In my own code, I'd like to be able to have a Model for other widgets, like QTexEdit or QSpinBox.

Can you elaborate a bit on what you want to do... give us an example of the kind of problem you’re trying to solve?

The whole idea of what Qt calls a “model” is to manage a collection of data — a list, a table, a tree or some hybrid of those — in a consistent way that separates maintaining the data from displaying the data. Since QTextEdits and QSpinBoxes don’t display collections... it’s unclear, at least to me, what you’re hoping to accomplish.

If you really do want to display a Qt model — something derived from QAbstractItemModel — in a custom GUI widget, then subclassing QAbstractItemView would usually be the way to go.

aamer4yu
3rd June 2010, 05:54
Have a look at QDataWidgetMapper


It seems, though, that this is only possible if I create a widget which inherits from something like QAbstractItemView. While there is tons of documentation on creating your own models and delegates, there doesn't seem to be much for views, and it seems like a lot more work.
If only you had gone through the Qt Demos properly !! :rolleyes:
Theres a simple data widget mapper in it.

Polnareff
3rd June 2010, 13:37
Can you elaborate a bit on what you want to do... give us an example of the kind of problem you’re trying to solve?

The whole idea of what Qt calls a “model” is to manage a collection of data — a list, a table, a tree or some hybrid of those — in a consistent way that separates maintaining the data from displaying the data. Since QTextEdits and QSpinBoxes don’t display collections... it’s unclear, at least to me, what you’re hoping to accomplish.

I'd like to, as you say, separate maintaining the data and displaying it. I'm working on software for a company, and we'd like to employ the MVC architecture to our code, or at least some form of it, so as to ensure scalability and, well, just plain organization :)


Have a look at QDataWidgetMapper

If only you had gone through the Qt Demos properly !! :rolleyes:
Theres a simple data widget mapper in it.
Haha, my mistake! I spent a lot of time looking through the demos my first week with Qt, but after a while, hadn't thought to return to them. I'll check it out. Thanks.

Coises
4th June 2010, 15:01
I'd like to, as you say, separate maintaining the data and displaying it. I'm working on software for a company, and we'd like to employ the MVC architecture to our code, or at least some form of it, so as to ensure scalability and, well, just plain organization

Qt’s “model/view” framework (http://doc.trolltech.com/latest/model-view-programming.html) can help when the model takes the form of a table, a tree or some hybrid of the two — the kind of thing where the view would be something like a QListView, a QTableView or a QTreeView. You certainly can create your own custom views, but if one of the standards isn’t at least close to what you want, there’s a good chance Qt’s model/view isn’t what you want.

That’s not to say you can’t program other forms of model/view or model/view/controller architecture in Qt — you just don’t use what Qt calls “model/view,” because it won’t help. You are largely on your own to divide your program logic in whatever way works best for you; but two Qt features that are particularly likely to be useful in general model/view/controller programming are Signals and Slots (http://doc.trolltech.com/latest/signalsandslots.html) and Properties (http://doc.trolltech.com/latest/properties.html).