Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Model View [Basic concept]
Forum Updated to NodeBB v4.3 + New Features

Model View [Basic concept]

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 361 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Calivernon
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • C Offline
        C Offline
        Calivernon
        wrote on last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Calivernon
            wrote on last edited by
            #5

            Ok Thank you SGaist,
            I have to work on my controller.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved