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. Addressbook example: separation of concerns
Forum Updated to NodeBB v4.3 + New Features

Addressbook example: separation of concerns

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 875 Views 2 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.
  • N nouwaarom

    Hi there,

    I am new to Qt but have done quite some programming in other frameworks.
    I was reading through the documentation about model view programming and stumbled upon the
    address book example.
    I do not understand why in the example the AddressWidget::addEntry() and AddressWidget::editEntry() manipulate the TableModel by row and column (insertRows, setData) instead of using a function in the TableModel, something like TableModel::addEntry(Contact contact) or tableModel::updateEntry().
    With the current approach the AddressWidget is aware of the internals of the TableModel. Which leads to more tangled code. Now if someone wants to reorder to layout of the table, both the AddressWidget and the model should be updated, which is easy to forget and can lead to some nasty bugs.
    If a function like TableModel::addEntry(Contact contact) is created the TableModel can update it's state while the AddressWidget stays clean.
    Am I missing something why the approach taken in the documentation is preferable to my suggestion?

    With kind regards,
    Elbert

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @nouwaarom
    Hello and welcome.

    You are indeed welcome to add whatever layer(s) of abstraction you wish for better code design. Qt is not a language, it's just a library of classes/functions. However you like your C++ programs to be on top of that is fine.

    The Qt examples are meant to illustrate Qt calls, in minimal-ish code size. They are not bad, but not claim to be the best abstraction you might choose to write for yourself.

    1 Reply Last reply
    2
    • N Offline
      N Offline
      nouwaarom
      wrote on last edited by
      #3

      Thanks for your response @JonB ,

      I understand that the documentation and examples are mainly focused on concisely explaining Qts functionality.
      But I think that a lot of beginning developers may not realize which design principles are used or are violated in the documentation and examples. By providing sub-optimal examples, they may see this as "the Qt way" and stick to that.
      What do you think about that?

      Pl45m4P JonBJ 2 Replies Last reply
      0
      • N nouwaarom

        Thanks for your response @JonB ,

        I understand that the documentation and examples are mainly focused on concisely explaining Qts functionality.
        But I think that a lot of beginning developers may not realize which design principles are used or are violated in the documentation and examples. By providing sub-optimal examples, they may see this as "the Qt way" and stick to that.
        What do you think about that?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #4

        @nouwaarom said in Addressbook example: separation of concerns:

        By providing sub-optimal examples

        The examples are mostly there to show how the Qt classes work and how to deal with them. They are not examples of how a well structured and clean C++ project with Qt should look like...
        Some of them can be used as a template, some of them are pure demo projects with focus on the topic, the example is all about.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1
        • N nouwaarom

          Thanks for your response @JonB ,

          I understand that the documentation and examples are mainly focused on concisely explaining Qts functionality.
          But I think that a lot of beginning developers may not realize which design principles are used or are violated in the documentation and examples. By providing sub-optimal examples, they may see this as "the Qt way" and stick to that.
          What do you think about that?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #5

          @nouwaarom
          I don't agree with your feelings, I do agree with @Pl45m4. He says the same as I did: examples are to illustrate Qt. For that I want to see minimal code showing just the essentials. If instead it went for "best style at all costs" I would (potentially) be wading through reams of code which --- while quite worthy --- may take me too much time and obscure the essence of what I need to grasp.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nouwaarom
            wrote on last edited by
            #6

            Thanks for your reply. I understand that you want to keep the documentation as compact and concise as possible. However, in the case of the addressbook example, the code is more complex and harder to read because setData is used to add data to the model from the widget, while the main purpose of setData is to be used when the model is editable, which this model is not.

            artwawA JonBJ 2 Replies Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Feel free to provide a patch.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • N nouwaarom

                Thanks for your reply. I understand that you want to keep the documentation as compact and concise as possible. However, in the case of the addressbook example, the code is more complex and harder to read because setData is used to add data to the model from the widget, while the main purpose of setData is to be used when the model is editable, which this model is not.

                artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by
                #8

                @nouwaarom said in Addressbook example: separation of concerns:

                setData is used to add data to the model from the widget, while the main purpose of setData is to be used when the model is editable

                No, the main purpose of this method is to apply the data to the record/row.
                There is a valid reason for adding data to be divided into two atomic steps: 1) add empty rows to the model; 2) set data to the given record/row.
                That reason is to provide elastic approach that can be ready used to derive model that suits the needs of the programmer.

                As my more experienced colleagues said - and I agree with them - you are most welcome to add to it, provide a patch, take it forward and develop further at your convenience especially if you feel that the framework is lacking some basic functionality.

                For more information please re-read.

                Kind Regards,
                Artur

                1 Reply Last reply
                1
                • N nouwaarom

                  Thanks for your reply. I understand that you want to keep the documentation as compact and concise as possible. However, in the case of the addressbook example, the code is more complex and harder to read because setData is used to add data to the model from the widget, while the main purpose of setData is to be used when the model is editable, which this model is not.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #9

                  @nouwaarom said in Addressbook example: separation of concerns:

                  while the main purpose of setData is to be used when the model is editable, which this model is not.

                  I don't follow? It is editable. The setData() updates a row.

                  1 Reply Last reply
                  1
                  • N Offline
                    N Offline
                    nouwaarom
                    wrote on last edited by
                    #10

                    Thanks for the replies. @Christian-Ehrlicher I will try that, but it might take some time to set everything up and submit a patch as I have not contributed to Qt yet.

                    What I meant by 'editable' is that the model is editable from the view.
                    For example if you add tableView->setEditTriggers(QAbstractItemView::DoubleClicked); when initializing when view.
                    Make sure TableModel::flags returns Qt::ItemIsEditable for the columns you want to be editable.
                    Then the view will call TableModel::setData for these columns when they are double clicked and their value is updated.
                    I experimented a bit with that here.

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • N nouwaarom

                      Thanks for the replies. @Christian-Ehrlicher I will try that, but it might take some time to set everything up and submit a patch as I have not contributed to Qt yet.

                      What I meant by 'editable' is that the model is editable from the view.
                      For example if you add tableView->setEditTriggers(QAbstractItemView::DoubleClicked); when initializing when view.
                      Make sure TableModel::flags returns Qt::ItemIsEditable for the columns you want to be editable.
                      Then the view will call TableModel::setData for these columns when they are double clicked and their value is updated.
                      I experimented a bit with that here.

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @nouwaarom said in Addressbook example: separation of concerns:

                      Then the view will call TableModel::setData for these columns when they are double clicked and their value is updated.

                      So what's different from the example then? That a separate widget instead a view updates the data? Who says that 'the view' must be a single widget?

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      3

                      • Login

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