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. custom model isn't completely working
Forum Updated to NodeBB v4.3 + New Features

custom model isn't completely working

Scheduled Pinned Locked Moved Solved General and Desktop
30 Posts 5 Posters 2.7k 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.
  • mzimmersM mzimmers

    @J-Hilk said in custom model isn't completely working:

    @mzimmers @JonB is correct,

    setData is only called, "automatically" if you change something in by the delegate via the Qt::EditRole

    OK, so in my case, where should that happen? I don't want my app to change the Switch setting; I only want it to send a request to the back end to make the change. Then, when I get a (successful) response, I change the value here:

    void EquipmentModel::processPostResponse()
    {
        QUuid uuid; // code for setting this has been removed.
        int i;
    
        i = m_list->getIndex(uuid);
        EquipmentItem itemFromList = m_list->getEquipmentItem(uuid);
    
        if (itemFromList.powerState() == Equipment::POWER_OFF) {
            itemFromList.setPowerState(Equipment::POWER_ON);
        } else {
            itemFromList.setPowerState(Equipment::POWER_OFF);
        }
        if (m_list->setItemAt(i, itemFromList)) {
            QModelIndex qmi = index(i, 0, QModelIndex());
            qDebug() << __PRETTY_FUNCTION__ << "emitting dataChanged(); qmi is" << qmi;
            emit dataChanged(qmi, qmi);
        }
    }
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #9

    @mzimmers
    In itself this seems OK to me. So what's with you complaining about setData() not getting called? Does your override of setData() do much the same as your setItemAt() code here? It doesn't do something else "special" you are relying on?

    mzimmersM 1 Reply Last reply
    0
    • JonBJ JonB

      @mzimmers
      In itself this seems OK to me. So what's with you complaining about setData() not getting called? Does your override of setData() do much the same as your setItemAt() code here? It doesn't do something else "special" you are relying on?

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

      @JonB based on what you said above, I replaced the line:

      emit dataChanged(qmi, qmi);
      

      with:

      setData(qmi, itemFromList.powerState(), StateRole);
      

      and now setData is indeed being called. Here's my setData() function:

      bool EquipmentModel::setData(const QModelIndex &index, const QVariant &value, int role)
      {
          bool rc = false;
      
          qDebug() << __PRETTY_FUNCTION__ << "role is" << role;
          do {
              if (m_list == nullptr) {
                  continue;
              }
              EquipmentItem item = m_list->equipment().at(index.row());
              switch (role) {
              case UuidRole:
                  item.setUuid(value.toUuid());
                  break;
              case NameRole:
                  item.setName(value.toString());
                  break;
              case FwVersionRole:
                  item.setFwVersion(value.toString());
                  break;
              case ModelIdRole:
                  item.setModelId(value.toString());
                  break;
              case StateRole:
                  item.setPowerState(Equipment::PowerState(value.toInt()));
                  qDebug() << __PRETTY_FUNCTION__ << "power state set to" << value.toInt();
                  break;
              }
      
              if (m_list->setItemAt(index.row(), item)) {
                  qDebug() <<  __PRETTY_FUNCTION__ << "setItemAt() returned successfully; role is" << role;
                  emit dataChanged(index, index, QVector<int>() << role);
                  rc = true;
              } else {
                  qWarning() << __PRETTY_FUNCTION__ << "error from setItemAt.";
              }
          } while (false);
          return rc;
      }
      

      The qDebug statements suggest that dataChanged is being emitted, so I guess I'm missing something downstream from this.

      JonBJ 1 Reply Last reply
      0
      • mzimmersM mzimmers

        @JonB based on what you said above, I replaced the line:

        emit dataChanged(qmi, qmi);
        

        with:

        setData(qmi, itemFromList.powerState(), StateRole);
        

        and now setData is indeed being called. Here's my setData() function:

        bool EquipmentModel::setData(const QModelIndex &index, const QVariant &value, int role)
        {
            bool rc = false;
        
            qDebug() << __PRETTY_FUNCTION__ << "role is" << role;
            do {
                if (m_list == nullptr) {
                    continue;
                }
                EquipmentItem item = m_list->equipment().at(index.row());
                switch (role) {
                case UuidRole:
                    item.setUuid(value.toUuid());
                    break;
                case NameRole:
                    item.setName(value.toString());
                    break;
                case FwVersionRole:
                    item.setFwVersion(value.toString());
                    break;
                case ModelIdRole:
                    item.setModelId(value.toString());
                    break;
                case StateRole:
                    item.setPowerState(Equipment::PowerState(value.toInt()));
                    qDebug() << __PRETTY_FUNCTION__ << "power state set to" << value.toInt();
                    break;
                }
        
                if (m_list->setItemAt(index.row(), item)) {
                    qDebug() <<  __PRETTY_FUNCTION__ << "setItemAt() returned successfully; role is" << role;
                    emit dataChanged(index, index, QVector<int>() << role);
                    rc = true;
                } else {
                    qWarning() << __PRETTY_FUNCTION__ << "error from setItemAt.";
                }
            } while (false);
            return rc;
        }
        

        The qDebug statements suggest that dataChanged is being emitted, so I guess I'm missing something downstream from this.

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

        @mzimmers
        This looks OK.

        Except that you call m_list->setItemAt(index.row(), item) unconditionally without looking at passed in role.

        Now I know QML uses special roles, and I have no idea whether it would do this, but if your setData() is called with, say, role == Qt::ForegroundRole you set the item in the list of values nonetheless. Usually you would test for the EditRole role. From QML you may need to include your explicit UuidRole etc. as well, I don't know. If QML never calls setData() with the various roles listed in https://doc.qt.io/qt-6/qt.html#ItemDataRole-enum this may not matter.

        mzimmersM 1 Reply Last reply
        0
        • JonBJ JonB

          @mzimmers
          This looks OK.

          Except that you call m_list->setItemAt(index.row(), item) unconditionally without looking at passed in role.

          Now I know QML uses special roles, and I have no idea whether it would do this, but if your setData() is called with, say, role == Qt::ForegroundRole you set the item in the list of values nonetheless. Usually you would test for the EditRole role. From QML you may need to include your explicit UuidRole etc. as well, I don't know. If QML never calls setData() with the various roles listed in https://doc.qt.io/qt-6/qt.html#ItemDataRole-enum this may not matter.

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

          @JonB my setData() is patterned after the example I mentioned in my first post. I see what you're saying (I think), and I should probably fix that, but it doesn't seem to be influencing the code execution (according to the telltales).

          Nowhere in my qml am I calling setData(); remember, I don't want my app to change anything (like the power state); merely to send a request to the back end for that change, and properly handle the response.

          Thanks...

          JoeCFDJ 1 Reply Last reply
          1
          • mzimmersM mzimmers

            @JonB my setData() is patterned after the example I mentioned in my first post. I see what you're saying (I think), and I should probably fix that, but it doesn't seem to be influencing the code execution (according to the telltales).

            Nowhere in my qml am I calling setData(); remember, I don't want my app to change anything (like the power state); merely to send a request to the back end for that change, and properly handle the response.

            Thanks...

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #13

            @mzimmers did you try to add
            beginResetModel();
            set list data.....
            endResetModel();

            mzimmersM 1 Reply Last reply
            1
            • JoeCFDJ JoeCFD

              @mzimmers did you try to add
              beginResetModel();
              set list data.....
              endResetModel();

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

              @JoeCFD hmm...I think you're on to something.

              I created a setList() function (also patterned after the example) that contains those calls:

              void EquipmentModel::setList(EquipmentList *list)
              {
                  beginResetModel();
                  if (m_list != nullptr) {
                      m_list->disconnect(this);
                  }
              
                  m_list = list;
              
                  if (m_list != nullptr) {
                      connect(m_list, &EquipmentList::preItemAppended, this, [=]() {
                          const int index = m_list->equipment().size();
                          beginInsertRows(QModelIndex(), index, index);
                      });
                      connect(m_list, &EquipmentList::postItemAppended, this, [=]() {
                          endInsertRows();
                      });
              
                      connect(m_list, &EquipmentList::preItemRemoved, this, [=](int index) {
                          beginRemoveRows(QModelIndex(), index, index);
                      });
                      connect(m_list, &EquipmentList::postItemRemoved, this, [=]() {
                          endRemoveRows();
                      });
                  }
                  endResetModel();
              }
              

              But...I never call it. I can try to modify my setData() to use it, though it will be tricky, as my list is a QObject, so I can't copy it. What do you think?

              EDIT:

              I modified this portion of my setData() as follows; still not getting the changes in the displays:

              beginResetModel();
              if (m_list->setItemAt(index.row(), item)) {
                  qDebug() <<  __PRETTY_FUNCTION__ << "setItemAt() returned successfully; role is" << role;
                  emit dataChanged(index, index, QVector<int>() << role);
                  rc = true;
              } else {
                  qWarning() << __PRETTY_FUNCTION__ << "error from setItemAt.";
              }
              endResetModel();
              

              I could move the begin/endResetModel() calls to the setItemAt() function, but I think the results would be the same, right?

              JoeCFDJ 1 Reply Last reply
              0
              • mzimmersM mzimmers

                @JoeCFD hmm...I think you're on to something.

                I created a setList() function (also patterned after the example) that contains those calls:

                void EquipmentModel::setList(EquipmentList *list)
                {
                    beginResetModel();
                    if (m_list != nullptr) {
                        m_list->disconnect(this);
                    }
                
                    m_list = list;
                
                    if (m_list != nullptr) {
                        connect(m_list, &EquipmentList::preItemAppended, this, [=]() {
                            const int index = m_list->equipment().size();
                            beginInsertRows(QModelIndex(), index, index);
                        });
                        connect(m_list, &EquipmentList::postItemAppended, this, [=]() {
                            endInsertRows();
                        });
                
                        connect(m_list, &EquipmentList::preItemRemoved, this, [=](int index) {
                            beginRemoveRows(QModelIndex(), index, index);
                        });
                        connect(m_list, &EquipmentList::postItemRemoved, this, [=]() {
                            endRemoveRows();
                        });
                    }
                    endResetModel();
                }
                

                But...I never call it. I can try to modify my setData() to use it, though it will be tricky, as my list is a QObject, so I can't copy it. What do you think?

                EDIT:

                I modified this portion of my setData() as follows; still not getting the changes in the displays:

                beginResetModel();
                if (m_list->setItemAt(index.row(), item)) {
                    qDebug() <<  __PRETTY_FUNCTION__ << "setItemAt() returned successfully; role is" << role;
                    emit dataChanged(index, index, QVector<int>() << role);
                    rc = true;
                } else {
                    qWarning() << __PRETTY_FUNCTION__ << "error from setItemAt.";
                }
                endResetModel();
                

                I could move the begin/endResetModel() calls to the setItemAt() function, but I think the results would be the same, right?

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #15

                @mzimmers
                https://doc.qt.io/qt-6/qabstractitemmodel.html#beginResetModel
                When a model is reset it means that any previous data reported from the model is now invalid and has to be queried for again. This also means that the current item and any selected items will become invalid.

                When a model radically changes its data it can sometimes be easier to just call this function rather than emit dataChanged() to inform other components when the underlying data source, or its structure, has changed.

                You must call this function before resetting any internal data structures in your model or proxy model.

                mzimmersM 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  @mzimmers
                  https://doc.qt.io/qt-6/qabstractitemmodel.html#beginResetModel
                  When a model is reset it means that any previous data reported from the model is now invalid and has to be queried for again. This also means that the current item and any selected items will become invalid.

                  When a model radically changes its data it can sometimes be easier to just call this function rather than emit dataChanged() to inform other components when the underlying data source, or its structure, has changed.

                  You must call this function before resetting any internal data structures in your model or proxy model.

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

                  @JoeCFD it's as though the QML property that reflects the state just isn't getting the message to update. I wonder if there's something wrong with how I'm setting that property:

                  Rectangle {
                      id: fakeSwitch
                      height: 30; width: 30
                      property bool switchState: equipmentModel.getEquipmentItem(pumpUuid).powerState // does this look OK?
                      color: switchState ? 'green' : 'gray'
                      MouseArea {
                          anchors.fill: parent
                          onClicked: {
                              fakeSwitch.switchState = !fakeSwitch.switchState
                              equipmentModel.sendSwitchChange(pumpUuid, fakeSwitch.switchState)
                          }
                      }
                  }
                  
                  JonBJ mzimmersM 2 Replies Last reply
                  0
                  • mzimmersM mzimmers

                    @JoeCFD it's as though the QML property that reflects the state just isn't getting the message to update. I wonder if there's something wrong with how I'm setting that property:

                    Rectangle {
                        id: fakeSwitch
                        height: 30; width: 30
                        property bool switchState: equipmentModel.getEquipmentItem(pumpUuid).powerState // does this look OK?
                        color: switchState ? 'green' : 'gray'
                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                fakeSwitch.switchState = !fakeSwitch.switchState
                                equipmentModel.sendSwitchChange(pumpUuid, fakeSwitch.switchState)
                            }
                        }
                    }
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #17

                    @mzimmers said in custom model isn't completely working:

                    property bool switchState: equipmentModel.getEquipmentItem(pumpUuid).powerState // does this look OK?

                    One thing. Your C++ code shows this is not a bool property, you test/set powerState() against Equipment::POWER_OFF/POWER_ON. Do these values convert correctly to bool?

                    mzimmersM 1 Reply Last reply
                    0
                    • mzimmersM mzimmers

                      @JoeCFD it's as though the QML property that reflects the state just isn't getting the message to update. I wonder if there's something wrong with how I'm setting that property:

                      Rectangle {
                          id: fakeSwitch
                          height: 30; width: 30
                          property bool switchState: equipmentModel.getEquipmentItem(pumpUuid).powerState // does this look OK?
                          color: switchState ? 'green' : 'gray'
                          MouseArea {
                              anchors.fill: parent
                              onClicked: {
                                  fakeSwitch.switchState = !fakeSwitch.switchState
                                  equipmentModel.sendSwitchChange(pumpUuid, fakeSwitch.switchState)
                              }
                          }
                      }
                      
                      mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #18

                      Another data point in this little mystery: I have 3 locations in my GUI that show the value of this model element. Two are in ordinary screens, and they're not updating correctly. The third is in a drawer, and it's always correct. The Switch code is virtually identical. Does the act of opening a drawer force an update to its contents? Is that why that one is always right?

                      JoeCFDJ J.HilkJ 2 Replies Last reply
                      0
                      • JonBJ JonB

                        @mzimmers said in custom model isn't completely working:

                        property bool switchState: equipmentModel.getEquipmentItem(pumpUuid).powerState // does this look OK?

                        One thing. Your C++ code shows this is not a bool property, you test/set powerState() against Equipment::POWER_OFF/POWER_ON. Do these values convert correctly to bool?

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

                        @JonB I think it is. I started using that FakeSwitch because I couldn't tell whether the real Switch was self-updating, and giving me a red herring. Here's the code from the real Switch, and its behavior is essentially the same (nothing in the custom switch affects this issue):

                        Switch_custom {
                            id: cardSwitch
                            visible: card.switchable
                            checked: equipmentModel.getEquipmentItem(uuid).powerState === 1
                            Layout.alignment: Qt.AlignRight
                            onClicked: {
                                console.log("EquipmentCard.qml: switch clicked; value is " + cardSwitch.checked)
                                equipmentModel.sendSwitchChange(card.uuid, cardSwitch.checked)
                            }
                        }
                        
                        
                        1 Reply Last reply
                        0
                        • mzimmersM mzimmers

                          Another data point in this little mystery: I have 3 locations in my GUI that show the value of this model element. Two are in ordinary screens, and they're not updating correctly. The third is in a drawer, and it's always correct. The Switch code is virtually identical. Does the act of opening a drawer force an update to its contents? Is that why that one is always right?

                          JoeCFDJ Offline
                          JoeCFDJ Offline
                          JoeCFD
                          wrote on last edited by JoeCFD
                          #20

                          @mzimmers What is the role type of drawer? I guess there is a default role.

                          mzimmersM 1 Reply Last reply
                          0
                          • JoeCFDJ JoeCFD

                            @mzimmers What is the role type of drawer? I guess there is a default role.

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

                            @JoeCFD I don't assign any role to anything in the drawer. I'm not sure how I'd even do that as it relates to the Switch it contains.

                            1 Reply Last reply
                            0
                            • mzimmersM mzimmers

                              Another data point in this little mystery: I have 3 locations in my GUI that show the value of this model element. Two are in ordinary screens, and they're not updating correctly. The third is in a drawer, and it's always correct. The Switch code is virtually identical. Does the act of opening a drawer force an update to its contents? Is that why that one is always right?

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by J.Hilk
                              #22

                              @mzimmers said in custom model isn't completely working:

                              Does the act of opening a drawer force an update to its contents?

                              possibly that opening the drawer recreates the drawer content, forcing a reload of the data each time its opened


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              mzimmersM 1 Reply Last reply
                              0
                              • J.HilkJ J.Hilk

                                @mzimmers said in custom model isn't completely working:

                                Does the act of opening a drawer force an update to its contents?

                                possibly that opening the drawer recreates the drawer content, forcing a reload of the data each time its opened

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

                                @J-Hilk well, in a sense, that's good news -- it would mean that my model isn't properly signaling changes at all, so at least the problem is consistent.

                                This is so weird -- it's almost as though the Switches on the different screens are independent of each other. Everything in the model seems to be working, from messaging the server, to accepting the response and (seemingly) sending the appropriate signals to QML.

                                I know this idea is a cheat, and I wouldn't want to use it in production, but...is there a way to force a QML component to update every time it becomes visible?

                                JoeCFDJ 1 Reply Last reply
                                0
                                • mzimmersM mzimmers

                                  @J-Hilk well, in a sense, that's good news -- it would mean that my model isn't properly signaling changes at all, so at least the problem is consistent.

                                  This is so weird -- it's almost as though the Switches on the different screens are independent of each other. Everything in the model seems to be working, from messaging the server, to accepting the response and (seemingly) sending the appropriate signals to QML.

                                  I know this idea is a cheat, and I wouldn't want to use it in production, but...is there a way to force a QML component to update every time it becomes visible?

                                  JoeCFDJ Offline
                                  JoeCFDJ Offline
                                  JoeCFD
                                  wrote on last edited by JoeCFD
                                  #24

                                  @mzimmers

                                   onVisibleChanged: {
                                        console.log( " visible =", visible )
                                        //do update if visible is true 
                                   }
                                  
                                  mzimmersM 1 Reply Last reply
                                  0
                                  • JoeCFDJ JoeCFD

                                    @mzimmers

                                     onVisibleChanged: {
                                          console.log( " visible =", visible )
                                          //do update if visible is true 
                                     }
                                    
                                    mzimmersM Offline
                                    mzimmersM Offline
                                    mzimmers
                                    wrote on last edited by
                                    #25

                                    @JoeCFD the Creator doesn't like the "do update" construct; am I supposed to replace update with something?

                                    JoeCFDJ 1 Reply Last reply
                                    0
                                    • mzimmersM mzimmers

                                      @JoeCFD the Creator doesn't like the "do update" construct; am I supposed to replace update with something?

                                      JoeCFDJ Offline
                                      JoeCFDJ Offline
                                      JoeCFD
                                      wrote on last edited by
                                      #26

                                      @mzimmers these are only texts. You do your update there.

                                      mzimmersM 1 Reply Last reply
                                      0
                                      • JoeCFDJ JoeCFD

                                        @mzimmers these are only texts. You do your update there.

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

                                        @JoeCFD OK, but what is the command to force the update?

                                        Here's what's happening: in my model, I make the change at the appropriate time:

                                        if (m_list->setItemAt(i, itemFromList)) {
                                            QModelIndex qmi = index(i, 0, QModelIndex());
                                            setData(qmi, itemFromList.powerState(), StateRole);
                                            qDebug() << __PRETTY_FUNCTION__ << "changed powerState to" << itemFromList.powerState();
                                        }
                                        

                                        And in a qml screen, I have this code:

                                        Text { text: "equipmentModel.getEquipmentItem(pumpUuid).powerState is " + equipmentModel.getEquipmentItem(pumpUuid).powerState }
                                        

                                        This line is NOT reflecting the change made above. Somewhere there's a disconnect between the change, and the QML, but it seems like the model is doing everything correctly.

                                        JoeCFDJ 1 Reply Last reply
                                        0
                                        • mzimmersM mzimmers

                                          @JoeCFD OK, but what is the command to force the update?

                                          Here's what's happening: in my model, I make the change at the appropriate time:

                                          if (m_list->setItemAt(i, itemFromList)) {
                                              QModelIndex qmi = index(i, 0, QModelIndex());
                                              setData(qmi, itemFromList.powerState(), StateRole);
                                              qDebug() << __PRETTY_FUNCTION__ << "changed powerState to" << itemFromList.powerState();
                                          }
                                          

                                          And in a qml screen, I have this code:

                                          Text { text: "equipmentModel.getEquipmentItem(pumpUuid).powerState is " + equipmentModel.getEquipmentItem(pumpUuid).powerState }
                                          

                                          This line is NOT reflecting the change made above. Somewhere there's a disconnect between the change, and the QML, but it seems like the model is doing everything correctly.

                                          JoeCFDJ Offline
                                          JoeCFDJ Offline
                                          JoeCFD
                                          wrote on last edited by
                                          #28

                                          @mzimmers in my code above you can print out the data value. If it is right, set it. If not, you have to find out the reason.

                                          mzimmersM 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