Łukasz Lew Łukasz Lew - 1 year ago 76
C++ Question

Model - View - Controler in Qt

I understand more or less how does MPV works.
But I don't get what classes:

  • QAbstractItemModel

  • QAbstractItemView

  • QAbstractItemDelegate

Can do for me?

If that is relevant, I'm using
with some elements (visual representation of game board) that user can interact with while the interaction logic (game rules) are encapsulated in other class.

Answer Source

AbstractItemModel QAbstractItemView QAbstractItemDelegate

Are from the "Mode/View framework"
This is a very powerful framework for the data part of your application, here is a presentation of the framework.


Is the base class for the model of the MVC. Has a global interface for accessing and altering the data and takes care of the Observable part.


Is the base class for the view of the MVC. Has aglobal interface for the view/selections part and it takes care of the Observer part. You don't have to worry about the observer pattern, the framework does it for you.


Is the base class for the controller of the MVC.
Is the Strategy pattern for painting, editing the elements, ...

QGraphicsScene / QGraphicsView

Are from the "The Graphics View Framework" and is independent of the Model/View framework.
This is also a very powerful framework for the graphics part.

The Scene

QGraphicsScene provides the Graphics View scene. The scene has the following responsibilities:

Providing a fast interface for managing a large number of items Propagating events to each item Managing item state, such as selection and focus handling Providing untransformed rendering functionality; mainly for printing

The View

QGraphicsView provides the view widget, which visualizes the contents of a scene. You can attach several views to the same scene, to provide several viewports into the same data set

If you want a Model to be visible in a QGraphicsView than you will have to write your own view based on the QAbstractItemView.
Take a QGraphicsView as view port widget QAbstractScrollArea::setViewport(QWidget * widget)) and then you can

  • add QAbstractItemView::rowsInserted,
  • remove QAbstractItemView::rowsAboutToBeRemoved
  • and change QAbstractItemView::dataChanged

the items in the scene. Don't forget to take care of the reset an layout change events.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download