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. update the data , not from the model
Forum Updated to NodeBB v4.3 + New Features

update the data , not from the model

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 1.2k 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.
  • O Offline
    O Offline
    orio
    wrote on last edited by
    #1

    i have a model view and data.
    the view is able to show an edit the data using the model as in all qt examples.

    the view and the model describe QTreeView

    but the data can be also changed from outside the model .
    say from a network message .

    it is clear that i dont want the part that changing the data from the network will know anything about the model or qt at all.

    so how am i suppose to call model beginResetModel() and endResetModel() before and after the change?

    JonBJ mzimmersM 2 Replies Last reply
    0
    • O orio

      i have a model view and data.
      the view is able to show an edit the data using the model as in all qt examples.

      the view and the model describe QTreeView

      but the data can be also changed from outside the model .
      say from a network message .

      it is clear that i dont want the part that changing the data from the network will know anything about the model or qt at all.

      so how am i suppose to call model beginResetModel() and endResetModel() before and after the change?

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

      @orio said in update the data , not from the model:

      it is clear that i dont want the part that changing the data from the network will know anything about the model or qt at all.

      Then you will have a real problem. You need a way to know that the data has changed externally if it is the data for your model. Otherwise you are likely to have to refresh the whole model from the external data, e.g. on a timer or when the view requests an operation.

      1 Reply Last reply
      1
      • O orio

        i have a model view and data.
        the view is able to show an edit the data using the model as in all qt examples.

        the view and the model describe QTreeView

        but the data can be also changed from outside the model .
        say from a network message .

        it is clear that i dont want the part that changing the data from the network will know anything about the model or qt at all.

        so how am i suppose to call model beginResetModel() and endResetModel() before and after the change?

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        @orio I'm not sure exactly what you're trying to do, but it's essential that you keep your model data current. As @JonB said, if you don't, you lose all the Qt magic that keeps your UI objects up to date, and you'll have to do it yourself.

        I'm currently working on an app that sends and receives network messages. My various models have slots connected to the network manager's finished() signal, and performs the updates there. Simple to code, and very reliable.

        O 1 Reply Last reply
        1
        • mzimmersM mzimmers

          @orio I'm not sure exactly what you're trying to do, but it's essential that you keep your model data current. As @JonB said, if you don't, you lose all the Qt magic that keeps your UI objects up to date, and you'll have to do it yourself.

          I'm currently working on an app that sends and receives network messages. My various models have slots connected to the network manager's finished() signal, and performs the updates there. Simple to code, and very reliable.

          O Offline
          O Offline
          orio
          wrote on last edited by orio
          #4

          @mzimmers

          my app receives network messages and can also edit and send them.

          so when editting message through the gui i use the qt model that has pointer to the actual data.

          sometime the editing of data is done by other component , that is not gui or qt related .

          but i still need somehow that the qt model will be updates

          JonBJ mzimmersM 2 Replies Last reply
          0
          • O orio

            @mzimmers

            my app receives network messages and can also edit and send them.

            so when editting message through the gui i use the qt model that has pointer to the actual data.

            sometime the editing of data is done by other component , that is not gui or qt related .

            but i still need somehow that the qt model will be updates

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

            @orio said in update the data , not from the model:

            but i still need somehow that the qt model will be updates

            Already answered, did you read my response above? Yes, you need to arrange to inform the Qt model that external updates have taken place. How else can it know? You have to find code/approach for that. Qt is not "magic", if you don't inform it data has changed it won't know. You seems to be saying you don't have a way of knowing what has happened and then asking for Qt to know, how do you suppose that would come about?

            1 Reply Last reply
            0
            • O orio

              @mzimmers

              my app receives network messages and can also edit and send them.

              so when editting message through the gui i use the qt model that has pointer to the actual data.

              sometime the editing of data is done by other component , that is not gui or qt related .

              but i still need somehow that the qt model will be updates

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #6

              @orio said in update the data , not from the model:

              sometime the editing of data is done by other component

              OK, then it seems you have two choices:

              • make that other component aware of your model, and have it manipulate the model through its public methods (add/set/delete items)
              • have your other component send a signal to the model, with an argument that contains all necessary information to update the model.

              The latter approach is the more "Qt" way of doing this, though it might be a little more involved.

              Either way, though, as @JonB has said, you must explicitly update the model. It has no way of magically knowing what's going on outside its boundaries.

              O 1 Reply Last reply
              0
              • mzimmersM mzimmers

                @orio said in update the data , not from the model:

                sometime the editing of data is done by other component

                OK, then it seems you have two choices:

                • make that other component aware of your model, and have it manipulate the model through its public methods (add/set/delete items)
                • have your other component send a signal to the model, with an argument that contains all necessary information to update the model.

                The latter approach is the more "Qt" way of doing this, though it might be a little more involved.

                Either way, though, as @JonB has said, you must explicitly update the model. It has no way of magically knowing what's going on outside its boundaries.

                O Offline
                O Offline
                orio
                wrote on last edited by orio
                #7

                @mzimmers

                Sorry for the bad English, maybe i am not understood.

                Qt is used only for the gui.
                Model is a qt class .

                The data is a "clean" c++ class , it should not know that it is being modeled or know qt model class , it does not have signals.

                I can somehow update the qt-model using an interface.

                But the model ask for signals before and after editing .

                I really dont understand how anyone else doesn't have this problem , all of you updating the data only through the qt model?

                jsulmJ JonBJ 2 Replies Last reply
                0
                • O orio

                  @mzimmers

                  Sorry for the bad English, maybe i am not understood.

                  Qt is used only for the gui.
                  Model is a qt class .

                  The data is a "clean" c++ class , it should not know that it is being modeled or know qt model class , it does not have signals.

                  I can somehow update the qt-model using an interface.

                  But the model ask for signals before and after editing .

                  I really dont understand how anyone else doesn't have this problem , all of you updating the data only through the qt model?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @orio You could write a Qt model class as wrapper for your plain C++ model class.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • O orio

                    @mzimmers

                    Sorry for the bad English, maybe i am not understood.

                    Qt is used only for the gui.
                    Model is a qt class .

                    The data is a "clean" c++ class , it should not know that it is being modeled or know qt model class , it does not have signals.

                    I can somehow update the qt-model using an interface.

                    But the model ask for signals before and after editing .

                    I really dont understand how anyone else doesn't have this problem , all of you updating the data only through the qt model?

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

                    @orio said in update the data , not from the model:

                    I really dont understand how anyone else doesn't have this problem , all of you updating the data only through the qt model?

                    No, some people/some of the time update the data in the model directly, without going through the model (e.g. setData()). But then, as stated previously, you must have some means of letting the model/views know what underlying data has been changed, or at worst that (potentially) the whole of the model has been altered.

                    I really don't understand how anyone cannot understand this is a requirement if you are going to use a Qt model against your own data!

                    I can somehow update the qt-model using an interface.

                    Well this is an improvement over earlier where you imply your outside data/code "will know anything about the model or qt at all" :)

                    But the model ask for signals before and after editing .

                    Don't know what this means to you. If you don't want your other code to know about Qt signals you can write, say as @jsulm says, some kind of very simple "wrapper" at the Qt side which your code calls and the wrapper does any signal emitting for you. But you still have to be prepared for your code to call this whenever it changes data directly.

                    mzimmersM 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @orio said in update the data , not from the model:

                      I really dont understand how anyone else doesn't have this problem , all of you updating the data only through the qt model?

                      No, some people/some of the time update the data in the model directly, without going through the model (e.g. setData()). But then, as stated previously, you must have some means of letting the model/views know what underlying data has been changed, or at worst that (potentially) the whole of the model has been altered.

                      I really don't understand how anyone cannot understand this is a requirement if you are going to use a Qt model against your own data!

                      I can somehow update the qt-model using an interface.

                      Well this is an improvement over earlier where you imply your outside data/code "will know anything about the model or qt at all" :)

                      But the model ask for signals before and after editing .

                      Don't know what this means to you. If you don't want your other code to know about Qt signals you can write, say as @jsulm says, some kind of very simple "wrapper" at the Qt side which your code calls and the wrapper does any signal emitting for you. But you still have to be prepared for your code to call this whenever it changes data directly.

                      mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #10

                      @JonB said in update the data , not from the model:

                      No, some people/some of the time update the data in the model directly, without going through the model (e.g. setData()

                      Hey Jon, this brings up an interesting point (one I'm struggling with in my other topic) -- who is responsible for calling setData()? From the Qt tutorial video, I was under the impression that this was needed when QML code was used to update the model. But after reading your post, it seems that the C++ model code must also call setData() when model in the data changes from an external source?

                      I have to admit it's a little confusing knowing when to call beginResetModel()/endResetModel() vs beginInsertRows()/addInsertRows(). I wish there were a tutorial that talked about this a little more in depth.

                      JonBJ 1 Reply Last reply
                      0
                      • mzimmersM mzimmers

                        @JonB said in update the data , not from the model:

                        No, some people/some of the time update the data in the model directly, without going through the model (e.g. setData()

                        Hey Jon, this brings up an interesting point (one I'm struggling with in my other topic) -- who is responsible for calling setData()? From the Qt tutorial video, I was under the impression that this was needed when QML code was used to update the model. But after reading your post, it seems that the C++ model code must also call setData() when model in the data changes from an external source?

                        I have to admit it's a little confusing knowing when to call beginResetModel()/endResetModel() vs beginInsertRows()/addInsertRows(). I wish there were a tutorial that talked about this a little more in depth.

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

                        @mzimmers
                        Remember I know nothing about QML.

                        Models provide setData(). The important thing is that will emit dataChanged(...), with the appropriate parameters, for the view and anything else to deal with. If you have other separate or internal code which alters the model's data, you don't have to call setData(), you can update the data structure and emit the dataChanged(). And similarly for insert/delete rows.

                        You would call begin/endInsert/DeleteRows() when you know, by whatever means, that row(s) have been inserted at a particular place. begin/endResetModel() allows for any change in the model: you don't know what/where, or there are lots of them. It causes the whole model to be invalidated and start again from scratch. Can be a bit "slow".

                        mzimmersM 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @mzimmers
                          Remember I know nothing about QML.

                          Models provide setData(). The important thing is that will emit dataChanged(...), with the appropriate parameters, for the view and anything else to deal with. If you have other separate or internal code which alters the model's data, you don't have to call setData(), you can update the data structure and emit the dataChanged(). And similarly for insert/delete rows.

                          You would call begin/endInsert/DeleteRows() when you know, by whatever means, that row(s) have been inserted at a particular place. begin/endResetModel() allows for any change in the model: you don't know what/where, or there are lots of them. It causes the whole model to be invalidated and start again from scratch. Can be a bit "slow".

                          mzimmersM Offline
                          mzimmersM Offline
                          mzimmers
                          wrote on last edited by
                          #12

                          @JonB I'd like to pursue this a bit further. Though the OP didn't post any code, I suspect this will be relevant to his problem as well.

                          I understand that the setData() implementation must emit dataChanged(), but...from where is setData() called? In my app, it's not getting called at all (judging from the telltales I put in my code). Yet, one of my models updates the QML just fine, and another one doesn't.

                          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