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. Modifying data outside a Model and View

Modifying data outside a Model and View

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 595 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.
  • E Offline
    E Offline
    EaccB
    wrote on last edited by
    #1

    I have a custom Model class which contains a pointer to a vector held elsewhere, and a View associated with the Model.

    Is it acceptable to modify (add, edit, delete, etc) the vector directly? I.e, not through the model or the view. If so, how would I notify the model that the data has been changed, or does it update automatically?

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

      Hi,

      Your model can't know of any changes to your vector unless you give an API to that vector that allows to get the information.

      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
      • E Offline
        E Offline
        EaccB
        wrote on last edited by EaccB
        #3

        Would it be best to have all data changes (which affect the view) handled from within the model? What's the best practise with this sort of thing? If I do a sort on the model, will it also sort the vector?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          See the documentation.
          When the complete data changed (whyever this should happen) you can use layoutChanged() but it should be avoided. Use dataChanged() wherever possible.
          Sorting has nothing to do with the QAbstractItemModel so no your data will not change.

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

          E 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            See the documentation.
            When the complete data changed (whyever this should happen) you can use layoutChanged() but it should be avoided. Use dataChanged() wherever possible.
            Sorting has nothing to do with the QAbstractItemModel so no your data will not change.

            E Offline
            E Offline
            EaccB
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in Modifying data outside a Model and View:

            Sorting has nothing to do with the QAbstractItemModel so no your data will not change.

            The documentation suggests that I can reimplement QAbstractItemModel::sort() to sort the data.

            I need both the vector and the model sorted.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Why do you need to sort your data when you can simply use a QSortFilterProxyModel?

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

              E 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                Why do you need to sort your data when you can simply use a QSortFilterProxyModel?

                E Offline
                E Offline
                EaccB
                wrote on last edited by
                #7

                @Christian-Ehrlicher

                If I use QSortFilterProxyModel, will it sort the vector too?

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

                  No, it acts on top of the model it proxies.

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

                  E 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    No, it acts on top of the model it proxies.

                    E Offline
                    E Offline
                    EaccB
                    wrote on last edited by
                    #9

                    @SGaist

                    So I would have to sort the vector manually then?

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @EaccB said in Modifying data outside a Model and View:

                      So I would have to sort the vector manually then?

                      Again: use a QSortFilterProxyModel when you want to display the data sorted.

                      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

                      • Login

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