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 53.5k 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
    #52

    My model looks like this:
    0_1535729444022_model1.PNG
    0_1535729450658_model2.PNG

    In parent method i use pointer for parent item but not reference.

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

      @VRonin @SGaist @JonB : I just debugged to see that after changing the value of the property via delegate does the index in the following code has the same value.

      QModelIndex index = createIndex(0, 1, categories_.at(0)->entryAt(0));
          this->setData(index, 11, Qt::EditRole);
      

      But it turns out that the index in the above is just a copy and when delegate changes the value it is not reflected in the index here. I think i am changing at a copy of the property but not the actual one.

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

          Can you show us the code for Delegate::setModelData?

          "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
          • VRoninV VRonin

            Can you show us the code for Delegate::setModelData?

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

            @VRonin

             void PropertyEditorItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
                    const auto currentItem = static_cast<ScenarioPropertyItem *>(index.internalPointer());
            
                    auto setup = [this, &model, &index](auto editor, auto usage) {
                        if (editor) {
                            usage(editor);
                        } else {
                            QStyledItemDelegate::setModelData(editor, model, index);
                        }
                    };
            
                    switch (currentItem->itemType()) {
                        case PropertyType::DOUBLE: {
                            setup(qobject_cast<QDoubleSpinBox *>(editor), [&index, &model](QDoubleSpinBox *editor) {
                                editor->interpretText();
                                model->setData(index, editor->value(), Qt::EditRole);
                            });
                            break;
                        }
                        case PropertyType::INTEGER: {
                            setup(qobject_cast<QSpinBox *>(editor), [&index, &model](QSpinBox *editor) {
                                editor->interpretText();
                                model->setData(index, editor->value(), Qt::EditRole);
                            });
                            break;
                        }
                        case PropertyType::STRING: {
                            setup(qobject_cast<QLineEdit *>(editor), [&index, &model](QLineEdit *editor) { model->setData(index, editor->text(), Qt::EditRole); });
                            break;
                        }
                        case PropertyType::BOOLEAN: {
                            setup(qobject_cast<QComboBox *>(editor),
                                  [this, &index, &model](QComboBox *editor) { model->setData(index, editor->currentText(), Qt::EditRole); });
                            break;
                        }
                        default:
                            QStyledItemDelegate::setModelData(editor, model, index);
                            break;
                    }
                }
            
            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by A Former User
              #57
              This post is deleted!
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #58

                Why are you calling createIndex in onRefreshRoadInfo ?

                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

                  Why are you calling createIndex in onRefreshRoadInfo ?

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

                  @SGaist In onRefereshRoadInfo function i get the new values which has to be shown in the view. So, here I want to update the values, and as i dont have the index's, i create index for every property and call setData to update the values. If this is not correct how should i update the values here?

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

                    @JonB @SGaist @VRonin : Any lead for my problem?

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

                      Please show more patience, allow at least 24 hours before bumping your own thread. People answering on this forum do it on a voluntary basis and might not live in the same time zone as you.

                      That said, I stumble upon the QJsonModel project. You might want to check it.

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

                        Please show more patience, allow at least 24 hours before bumping your own thread. People answering on this forum do it on a voluntary basis and might not live in the same time zone as you.

                        That said, I stumble upon the QJsonModel project. You might want to check it.

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

                        @SGaist I will keep in mind :). I saw the project and looks like my current implementation looks but, the project does not have the one thing i am looking for i.e. updating of data inside the code i.e. onRefereshRoadInfo.

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

                          How is it possible that even after changing the model data value in a hard way and emitting reset model signals does not refresh the view.

                          void ScenarioPropertiesModel::onRefreshRoadInfo(const NewData& roadInfo) {
                              emit beginResetModel();
                              categories_.at(0)->entryAt(0)->setValue(11111);
                              emit endResetModel();
                          }
                          

                          Could it be that index function implementation is wrong? This is my index implementation:

                          
                          QModelIndex Model::index(int row, int column, const QModelIndex& parent /*= QModelIndex()*/) const {
                              if (!hasIndex(row, column, parent)) {
                                  return QModelIndex();
                              }
                          
                              if (!parent.isValid()) {
                                  return createIndex(row, column, categories_.at(row).get());
                              }
                          
                              const auto currentCategory = static_cast<CategoryItem*>(parent.internalPointer());
                              ScenarioPropertyItem* currentItem = currentCategory->entryAt(row);
                          
                              if (currentItem) {
                                  return createIndex(row, column, currentItem);
                              } else {
                                  return QModelIndex();
                              }
                          }
                          
                          1 Reply Last reply
                          0
                          • VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #64

                            It still not clear to me in what component the problem is. Could you try to remove your custom delegate and see if it updates the values? if it doesn't then it's a model problem but at least we know where to focus

                            "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
                            • VRoninV VRonin

                              It still not clear to me in what component the problem is. Could you try to remove your custom delegate and see if it updates the values? if it doesn't then it's a model problem but at least we know where to focus

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

                              @VRonin: Hi Ronin, thnx for the reply. Even without delegates it is not working. I believe it lies in model. I am updating a copy of the model but not the model.
                              Here below, the model pointer is always in the old state, even after changing the values in my model class.

                              void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)
                              

                              For infor: I have hosted the project in Github: https://github.com/vinayrajputh/qtmodelview

                              VRoninV 1 Reply Last reply
                              0
                              • ? A Former User

                                @VRonin: Hi Ronin, thnx for the reply. Even without delegates it is not working. I believe it lies in model. I am updating a copy of the model but not the model.
                                Here below, the model pointer is always in the old state, even after changing the values in my model class.

                                void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)
                                

                                For infor: I have hosted the project in Github: https://github.com/vinayrajputh/qtmodelview

                                VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by
                                #66

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

                                For infor: I have hosted the project in Github: https://github.com/vinayrajputh/qtmodelview

                                Would have been nice if it compiled. Can you upload the ui and json file too?

                                "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
                                1
                                • VRoninV VRonin

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

                                  For infor: I have hosted the project in Github: https://github.com/vinayrajputh/qtmodelview

                                  Would have been nice if it compiled. Can you upload the ui and json file too?

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

                                  @VRonin : Done.

                                  1 Reply Last reply
                                  0
                                  • VRoninV Offline
                                    VRoninV Offline
                                    VRonin
                                    wrote on last edited by
                                    #68

                                    I'm concentrating on the model only at the moment but it works perfectly for me.
                                    The only thing that I would flag is that the json that you provided has all "ReadOnly": true so it won't allow changes unless you change that to false.

                                    Could you try this fork and tell me what is that you expect to happen that is not happening?

                                    "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
                                    • VRoninV VRonin

                                      I'm concentrating on the model only at the moment but it works perfectly for me.
                                      The only thing that I would flag is that the json that you provided has all "ReadOnly": true so it won't allow changes unless you change that to false.

                                      Could you try this fork and tell me what is that you expect to happen that is not happening?

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

                                      @VRonin: Now there is a function with following signature

                                      void ScenarioPropertiesModel::onRefreshRoadInfo(const NewData& roadInfo) {}
                                      

                                      Now i would ask you to set the model data (properties) to the new values present in roadInfo variable(or to a random number) in the above function and display it in the view.

                                      My usecase is that, i will have a street and when user clicks on the street onRefreshRoadInfo will be called with the values which are actually coordinates and these i want to show.

                                      1 Reply Last reply
                                      0
                                      • VRoninV Offline
                                        VRoninV Offline
                                        VRonin
                                        wrote on last edited by
                                        #70

                                        So you are confirming that, so far, the fork above is behaving correctly?

                                        "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
                                        • VRoninV VRonin

                                          So you are confirming that, so far, the fork above is behaving correctly?

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

                                          @VRonin : Yes, it works correctly. :)

                                          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