Solved Model View [Basic concept]
-
Hello,
I am trying to understand the Model View architecture, what I did works but I am not sure that is correctly done in a real "Model-view" way of thinking, I would like your advices.
My data-Model is the sudoku class, I just wrote the basic here:
/* class with the data structure */ class Sudoku { private: int** m_aValues; // usually array 9x9 /* ... */ public: Sudoku() { /* ... */ } } /* Class to display the data */ class CScene : public QGraphicsScene { /* ... */ } /* class managing the main window */ /* all toolBars, menus, files ... */ class MainWindow : public QMainWindow { private: QGraphicsView* m_pView; CScene* m_pScene; CSudoku* m_pSudoku; (the model) QMenu* m_pMenu; QToolBar* m_pToolBar; /* ... */ } /* class for the cells in the 9x9 grid to be added to the scene */ class Cell : public QGraphicsItem { private: int m_row; (coordinates in the grid) int m_col; int m_sizeCells; public: Cell() /* ... */ QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); }
My Question is: "Where the display process has to be done?"
What I did is that I do everything in MainWindow:
MainWindow owns data and scene so I scan all values in sudoku and create Cells (empty or with values) and add them to the scene.But I could also, from MainWindow, send the data (m_pSudoku) to the scene and create cells there and add them to the scene (the process would be in the scene).
OR
I could also send the (m_pScene) to the Sudoku data, read the data there and create/add the graphics items (the process would be in the Sudoku Data).It does not change much for such a small program but I would like to know what is the "good" practice.
Thank you for your help.
-
Hi,
Keep the data completely separated of the view. The model provides the data. The view is responsible to grab them from the model but the model has no idea about the view(s) that reads it.
-
@SGaist: Thank you I understand.
I have another question about this structure:
I thought that, in my case, Data and Model was the same thing, but do I need one more layer "Model" to link View and Data?
The MainWindow could be the Model but as its first purpose is to manage menus, toolbars etc... I am not sure that would respect the Model-View architecture? -
MainWindow might manage the model and the view that will use that model but MainWindow is not the model.
The more classic pattern is MVC, Model View Controller.
-
Ok Thank you SGaist,
I have to work on my controller.