Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Announcements
  4. How to architect a Qt/C++ Application

How to architect a Qt/C++ Application

Scheduled Pinned Locked Moved Unsolved Announcements
architecturestructure
7 Posts 3 Posters 10.2k 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.
  • A Offline
    A Offline
    AlexFagrell
    wrote on 11 Jan 2019, 17:11 last edited by
    #1

    Hi guys,
    I've previously posted here regarding the series "Crash course in Qt for C++ developers" and have this week released the 7th part in the series. This part covers "how to organise and structure a Qt Application". Hope you like it!

    https://www.cleanqt.io/blog/crash-course-in-qt-for-c%2B%2B-developers,-part-7

    I would be very happy if you had any feedback.

    Cheers,
    Alex

    K 1 Reply Last reply 12 Jan 2019, 04:06
    1
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Jan 2019, 20:37 last edited by
      #2

      Hi,

      While it's a good idea to separate the GUI from the processing parts, you can also build a library of your GUI elements so you can also test them and re-use them if you have several applications in your projects.

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

      A 1 Reply Last reply 12 Jan 2019, 15:30
      3
      • A AlexFagrell
        11 Jan 2019, 17:11

        Hi guys,
        I've previously posted here regarding the series "Crash course in Qt for C++ developers" and have this week released the 7th part in the series. This part covers "how to organise and structure a Qt Application". Hope you like it!

        https://www.cleanqt.io/blog/crash-course-in-qt-for-c%2B%2B-developers,-part-7

        I would be very happy if you had any feedback.

        Cheers,
        Alex

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 12 Jan 2019, 04:06 last edited by
        #3

        @AlexFagrell

        If I may complain:

        1. If you're taking ownership of the model, then don't hold the reference to the object in a QPointer, use either QScopedPointer or a raw pointer. The former is for when you don't possess ownership.

        2. I'd not put a controller inside a library. The controller has to know about the view and the model. As such it makes little sense hiding it behind a library interface. I'd put the controller in the application drop any unnecessary abstraction(s) and keep the models and (possibly) the reusable UI components in the library.

        @SGaist said in How to architect a Qt/C++ Application:

        While it's a good idea to separate the GUI from the processing parts, you can also build a library of your GUI elements so you can also test them and re-use them if you have several applications in your projects.

        +100

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • S SGaist
          11 Jan 2019, 20:37

          Hi,

          While it's a good idea to separate the GUI from the processing parts, you can also build a library of your GUI elements so you can also test them and re-use them if you have several applications in your projects.

          A Offline
          A Offline
          AlexFagrell
          wrote on 12 Jan 2019, 15:30 last edited by
          #4

          @SGaist

          Hi,
          Thanks for your comment! Agreed, the GUI components could indeed reside in a separate library so that they can be easily reused for multiple applications. Though similar idea could be said about the back-end library: it could be further decoupled to better serve multiple applications, as each application most likely only require a subset of the logic.

          The goal of the post was to not cover all use cases, but the focus was on "one" application presented using two different technologies (front-ends). The post is more of an introduction to architecture, as I'm sure additional separations will be required when the application grows (or when multiple applications are created using the same back-end). That said, I can see why this could be valuable to mention in order to further scale the application. I'll add a comment or two regarding this to the post. Thanks!

          @kshegunov

          Thanks for the feedback!

          1. I agree, conceptually what I've implemented is incorrect, so I'll change and update it. Thanks!

          2. For this architecture, the Controller doesn't know about the view; only about the model. As mentioned in the post "the controllers in this design are different from the traditional definition (i.e. as defined in Smalltalk). The controllers here can be considered routers that receive user interactions but also manage the flow of the application by forwarding calls and signals to the relevant model(s) and other controller(s)". See the dependency diagram shown in the post for clarification.

          That said, I believe further separation can be done by decoupling the controllers from the backend into a separate library, but IMHO the front-end should be very thin and not bundle upped with controllers.

          In addition, as I mentioned in the post, there are many different ways to shape the architecture of a Qt application. The solution presented in it is only one of many, and it's most likely not a perfect fit for all domains. Your suggestion sounds a bit like Apple's approach to MVC which I'm sure also works great! See: https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html

          Cheers,
          Alex

          K 1 Reply Last reply 14 Jan 2019, 10:53
          0
          • A AlexFagrell
            12 Jan 2019, 15:30

            @SGaist

            Hi,
            Thanks for your comment! Agreed, the GUI components could indeed reside in a separate library so that they can be easily reused for multiple applications. Though similar idea could be said about the back-end library: it could be further decoupled to better serve multiple applications, as each application most likely only require a subset of the logic.

            The goal of the post was to not cover all use cases, but the focus was on "one" application presented using two different technologies (front-ends). The post is more of an introduction to architecture, as I'm sure additional separations will be required when the application grows (or when multiple applications are created using the same back-end). That said, I can see why this could be valuable to mention in order to further scale the application. I'll add a comment or two regarding this to the post. Thanks!

            @kshegunov

            Thanks for the feedback!

            1. I agree, conceptually what I've implemented is incorrect, so I'll change and update it. Thanks!

            2. For this architecture, the Controller doesn't know about the view; only about the model. As mentioned in the post "the controllers in this design are different from the traditional definition (i.e. as defined in Smalltalk). The controllers here can be considered routers that receive user interactions but also manage the flow of the application by forwarding calls and signals to the relevant model(s) and other controller(s)". See the dependency diagram shown in the post for clarification.

            That said, I believe further separation can be done by decoupling the controllers from the backend into a separate library, but IMHO the front-end should be very thin and not bundle upped with controllers.

            In addition, as I mentioned in the post, there are many different ways to shape the architecture of a Qt application. The solution presented in it is only one of many, and it's most likely not a perfect fit for all domains. Your suggestion sounds a bit like Apple's approach to MVC which I'm sure also works great! See: https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html

            Cheers,
            Alex

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 14 Jan 2019, 10:53 last edited by
            #5

            @AlexFagrell said in How to architect a Qt/C++ Application:

            For this architecture, the Controller doesn't know about the view; only about the model. As mentioned in the post "the controllers in this design are different from the traditional definition (i.e. as defined in Smalltalk). The controllers here can be considered routers that receive user interactions but also manage the flow of the application by forwarding calls and signals to the relevant model(s) and other controller(s)". See the dependency diagram shown in the post for clarification.

            I did see the diagram, it does look like the classical triangle you'd expect in MVC. I haven't looked at the actual code to comment further, though, so it's something I suppose I should do.

            As a side comment you have this:

            Qt's main event loop is entered by running the exec() member function on an instance of a QCoreApplication, a QGuiApplication or a QApplication.

            in part one of your series, which is incorrect (and you should fix). QCoreApplication::exec is a static function and as such is not run on any instance. I've been complaining about this for some time - this misconception is spread through the docs' examples/snippets so people are left with the impression exec is a method, it's not. I have always thought that it's much better to be explicit about it (especially in the docs) and be written as:

            return QApplication::exec();
            

            Your suggestion sounds a bit like Apple's approach to MVC which I'm sure also works great!

            Well, that's the "classical" understanding of what a controller is. In Qt's examples you most often have only a model-view structure, where the view acts as the controller.

            Read and abide by the Qt Code of Conduct

            A 1 Reply Last reply 14 Jan 2019, 12:03
            1
            • K kshegunov
              14 Jan 2019, 10:53

              @AlexFagrell said in How to architect a Qt/C++ Application:

              For this architecture, the Controller doesn't know about the view; only about the model. As mentioned in the post "the controllers in this design are different from the traditional definition (i.e. as defined in Smalltalk). The controllers here can be considered routers that receive user interactions but also manage the flow of the application by forwarding calls and signals to the relevant model(s) and other controller(s)". See the dependency diagram shown in the post for clarification.

              I did see the diagram, it does look like the classical triangle you'd expect in MVC. I haven't looked at the actual code to comment further, though, so it's something I suppose I should do.

              As a side comment you have this:

              Qt's main event loop is entered by running the exec() member function on an instance of a QCoreApplication, a QGuiApplication or a QApplication.

              in part one of your series, which is incorrect (and you should fix). QCoreApplication::exec is a static function and as such is not run on any instance. I've been complaining about this for some time - this misconception is spread through the docs' examples/snippets so people are left with the impression exec is a method, it's not. I have always thought that it's much better to be explicit about it (especially in the docs) and be written as:

              return QApplication::exec();
              

              Your suggestion sounds a bit like Apple's approach to MVC which I'm sure also works great!

              Well, that's the "classical" understanding of what a controller is. In Qt's examples you most often have only a model-view structure, where the view acts as the controller.

              A Offline
              A Offline
              AlexFagrell
              wrote on 14 Jan 2019, 12:03 last edited by
              #6

              @kshegunov Thanks again for your feedback, appreciate it! I'll rephrase it so that it's clear that it's a static member function and update the code accordingly.

              Regarding the controller, I believe there are so many different MVCs and so many variations of the controller and perspectives that it has effectively no 'classical' meaning. Another example is the one shown on the wiki https://en.wikipedia.org/wiki/Model–view–controller which doesn't depend on the view.

              The model-view structure works great for directly presenting data, e.g. in a list/table/tree. However, IMO less so for the overall architecture/design of the application.

              Cheers,
              Alex

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 14 Jan 2019, 21:38 last edited by
                #7

                The controller concept is not specific to the MVC. MVC uses one of its incarnation.

                You can have a controller class that does the abstraction and handling of the lower level bits of you application while your GUI connects directly to it happily ignoring the gory details. No model required.

                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
                1

                5/7

                14 Jan 2019, 10:53

                • Login

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