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. How to modify the data in the model and update the view when new data is received from external?
Forum Updated to NodeBB v4.3 + New Features

How to modify the data in the model and update the view when new data is received from external?

Scheduled Pinned Locked Moved Solved General and Desktop
76 Posts 4 Posters 50.8k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    Hello everyone,
    I have an model subclass from QAbstractmodel. I have created the model and set to the QTreeview. By default the values are empty. When the user left clicks the mouse set of new values arrives which has to be updated in the model and notify the view to refresh. In my model subclass i have a member function newValuesarrived(QMap<QString, QString> listOfNewValues); In this function i have to update the model and notify the view. How can this be achieved?

    ---- update---
    Due to legal reason i have to take out the code.

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

      Hi,

      You should start by reading the Model/View Programming chapter of Qt's documentation. You'll find the complete concept explained. As for refreshing the view, if you implement the model correctly, then it will be done automatically for you.

      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
      5
      • SGaistS SGaist

        Hi,

        You should start by reading the Model/View Programming chapter of Qt's documentation. You'll find the complete concept explained. As for refreshing the view, if you implement the model correctly, then it will be done automatically for you.

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @SGaist : It does not explain about handling new data from extern source.

        JonBJ 1 Reply Last reply
        0
        • ? A Former User

          @SGaist : It does not explain about handling new data from extern source.

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

          @VInay123
          It depends what you mean by "external source".

          • Whatever source you get data from, your job is to use the model update methods, and the view will get refreshed.
          • If you mean that new data arrives up at a database, Qt does not know about that.

          In my model subclass i have a member function newValuesarrived(QMap<QString, QString> listOfNewValues); In this function i have to update the model and notify the view. How can this be achieved?

          So you write the code to call methods like insertRows() & setData() to update the model from your new list of values. The view will get updated from the model.

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

            What external source do you have in mind ?

            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
            • SGaistS SGaist

              What external source do you have in mind ?

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by A Former User
              #6

              @SGaist I have uploaded the code. Actually i read a json file which has property nam, address, readonly and few more things. In tree form somethng like thi:
              ->Categoryitem1
              --->scenarioitem1(key and value)
              ---->scenarioitem2(key and value)

              Now i get new values in info variable which has to be updated in the scenarioitem1 and 2. Everytime user leftclicks i get new values which has to be updated in the view.

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

                Then update your model and then call the dataChanged signal.

                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
                4
                • SGaistS SGaist

                  Then update your model and then call the dataChanged signal.

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  @SGaist :I understand tht i need to send dataChanged signal. Where should i update my model? I mean at which place in my code? Secondly for dataChanged i need index values and how would i get it. If you could show me in my code will be helpful. Thankyou

                  JonBJ 1 Reply Last reply
                  0
                  • ? A Former User

                    @SGaist :I understand tht i need to send dataChanged signal. Where should i update my model? I mean at which place in my code? Secondly for dataChanged i need index values and how would i get it. If you could show me in my code will be helpful. Thankyou

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

                    @VInay123
                    So when you read the JSON file that's where you will update your model.
                    For the index values: yes, you have to decide where in your model your data is to go, and from that you will have the index values.

                    ? 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @VInay123
                      So when you read the JSON file that's where you will update your model.
                      For the index values: yes, you have to decide where in your model your data is to go, and from that you will have the index values.

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      @JonB : As you see in my code i read the Json file only ones i..e during initialization with empty values. Later i just want to update the values but not read again the file. Second point how would you suggest me to implement in my design or code? Example would be nice. Thank you.

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

                        Re-implement setData

                        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
                        2
                        • SGaistS SGaist

                          Re-implement setData

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by A Former User
                          #12

                          @SGaist: How would i provide the index values? I am really sorry, because somehow i am not able to understand this trick of updating my data (categories_) in model, i.e. how do i get index from my data and call setData? I would really appreciate if you could show in my code? This would then later emit dataChanged which will achieve my goal.

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

                            How exactly are you getting new data ?
                            What are these new data ?
                            One object update ?
                            Several objects update ?
                            How do they fit in your model internal representation ?

                            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
                            0
                            • SGaistS SGaist

                              How exactly are you getting new data ?
                              What are these new data ?
                              One object update ?
                              Several objects update ?
                              How do they fit in your model internal representation ?

                              ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by A Former User
                              #14

                              @SGaist : I am getting this data when user clicks left mouse button. I have slot, so when user clicks button some magic is done and later i save in model member variable Info_.
                              The NewStructInfo looks like this:
                              struct NewStructInfo{
                              double x;
                              double y;
                              } Info_;
                              So now my model data categories_ looks something like this:
                              ```
                              Property ----------------------------------------- Value
                              -->Coordinates(categoryitem pointer)
                              ------X (scenariopropertyitem1 pointer type) ------ "Empty"
                              ------Y (scenariopropertyitem pointer type) -------- "Empty"

                              The above structure you can see in parsejsonobject member function. Now in the above categories_ model data i have iterate along category->secnariopropertyitem and look for the name i,e,

                              if(scenariopropertyitem->name == x)
                              // scenariopropertyitem pointer has name as class member
                              scenariopropertyitem->setValue(QVariant(Info_.X));
                              Now somehow dataChanged(??,??,Qt::displayrole) has to be called.

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

                                Since you have to go through your model to find the correct item to update, you should already have the index matching the scenariopropertyitem you found, no ?

                                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
                                • SGaistS SGaist

                                  Since you have to go through your model to find the correct item to update, you should already have the index matching the scenariopropertyitem you found, no ?

                                  ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by A Former User
                                  #16

                                  @SGaist : How would i access it or get the index of the item? Meanwhile would you suggest me to use third option from this answer: https://stackoverflow.com/questions/29921041/getting-the-index-for-a-given-item-in-a-qtreeview-model-application.
                                  Could you provide an example?

                                  1 Reply Last reply
                                  0
                                  • ? Offline
                                    ? Offline
                                    A Former User
                                    wrote on last edited by A Former User
                                    #17
                                    This post is deleted!
                                    1 Reply Last reply
                                    0
                                    • ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #18
                                      This post is deleted!
                                      1 Reply Last reply
                                      0
                                      • VRoninV Offline
                                        VRoninV Offline
                                        VRonin
                                        wrote on last edited by
                                        #19

                                        What I always suggest is that if you are not 100% confident in your model design (and, believe me, it's a hard) just use QAbstractItemModel* model = new QStandardItemModel(parent). Then use insertRows, insertColumns, and setData to setup your model

                                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                        ~Napoleon Bonaparte

                                        On a crusade to banish setIndexWidget() from the holy land of Qt

                                        1 Reply Last reply
                                        0
                                        • ? Offline
                                          ? Offline
                                          A Former User
                                          wrote on last edited by
                                          #20

                                          In my Model.cpp i did the following:

                                              emit beginResetModel();
                                              for (auto& category : categories_) {
                                                  category->entryAt(0)->setValue(1);
                                                  // for testing  i just set randomly my data value to 1
                                              }
                                              emit endResetModel();
                                          

                                          But after above changes the view is not displaying the new values.

                                          JonBJ 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