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. MVC Example in Qt
QtWS25 Last Chance

MVC Example in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
oopsmvcdesign pattern
3 Posts 3 Posters 3.1k Views
  • 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.
  • R Offline
    R Offline
    Rohith
    wrote on last edited by
    #1

    Hi i am trying to learn MVC architecture and i am able to understand the the theoretical concept very well but i am unable to try it practically in a full fledged manner..!
    Can any one please post an example of MVC architecture by using Qt so that it helps to understand the concept in brief

    Thanks in advance,
    Rohith.G

    1 Reply Last reply
    0
    • A Offline
      A Offline
      asanka424
      wrote on last edited by
      #2

      MVC implementations in C++ is not that frequent. Most MVC implementations I have seen (done) are done by using the 'Controller' as the middle man between 'View' and the 'Model'. That is Model and View are completely de-coupled and communicate via Controller.

      However in my experiece, MVC cannot be implemented as cleanly as in a web application for a desktop application. Main reasone is that all UI widgets have some sort of controller embedded in already.

      For that reason Qt has something called Model-View architecture which is not the MVC pattern but something similar to de-couple your data from the view.

      This reply based on my personal experience and I am sure other people will have other opinions.

      Thanks

      kshegunovK 1 Reply Last reply
      0
      • A asanka424

        MVC implementations in C++ is not that frequent. Most MVC implementations I have seen (done) are done by using the 'Controller' as the middle man between 'View' and the 'Model'. That is Model and View are completely de-coupled and communicate via Controller.

        However in my experiece, MVC cannot be implemented as cleanly as in a web application for a desktop application. Main reasone is that all UI widgets have some sort of controller embedded in already.

        For that reason Qt has something called Model-View architecture which is not the MVC pattern but something similar to de-couple your data from the view.

        This reply based on my personal experience and I am sure other people will have other opinions.

        Thanks

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @Rohith

        A controller is simply an object that controls both the model and the view. The following brief example should get you started:

        class MyController : public QObject
        {
            Q_OBJECT
        public:
            MyController(QObject * parent = NULL)
                : QObject(parent)
            {
                view.setModel(&model);
            }
        
            int exec()
            {
                view.show();
                return QApplication::exec();
            }
        
        private:
            // ...
            QTreeView view;
            QFileSystemModel model;
        };
        

        And you use simply by creating the object and invoking the appropriate method(s).

        int main(int argc, char ** argv)
        {
            QApplication app(argc, argv);
        
            MyController controller;
            return controller.exec();
        }
        

        @asanka424

        However in my experiece, MVC cannot be implemented as cleanly as in a web application for a desktop application. Main reasone is that all UI widgets have some sort of controller embedded in already.
        For that reason Qt has something called Model-View architecture which is not the MVC pattern but something similar to de-couple your data from the view.

        Qt only applies the view as controller, but doesn't actually "require" the user programmer to follow. The same effect as and I'd say better could be obtained by separating the logic. One thing that springs to mind: no need to derive a whole class only to have a form initialized in the constructor.

        The only problem with MVC is that it's used with and without reason (as all "architectural" and "design" patterns are), because people are mislead to believe them as one size fits all solution.

        Kind regards.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1

        • Login

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