Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Dynamic Upadate of data to a ListModel
Forum Updated to NodeBB v4.3 + New Features

Dynamic Upadate of data to a ListModel

Scheduled Pinned Locked Moved QML and Qt Quick
37 Posts 4 Posters 14.0k Views 2 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.
  • m.sueM Offline
    m.sueM Offline
    m.sue
    wrote on last edited by
    #7

    @saitej: you had an addDatamethod, so I suspect you would really add some data.
    @raven-worx: Ah, I see, then they probably emit the \*AboutToBe\* signals, as somehow, of course, the widget must know about these actions.
    -Michael.

    saitejS raven-worxR 2 Replies Last reply
    0
    • m.sueM m.sue

      @saitej: you had an addDatamethod, so I suspect you would really add some data.
      @raven-worx: Ah, I see, then they probably emit the \*AboutToBe\* signals, as somehow, of course, the widget must know about these actions.
      -Michael.

      saitejS Offline
      saitejS Offline
      saitej
      wrote on last edited by
      #8

      @m.sue

      Ya for adddata method I am already using the beginInsertRows :)

      1 Reply Last reply
      0
      • raven-worxR raven-worx

        @m.sue said in Dynamic Upadate of data to a ListModel:

        This will probably result in calling the dataChanged() signal indirectly :-)

        actually no.
        The dataChanged() signal is for the case (surprise) when the data (of an existing item) has changed .
        The methods mentioned by you should be self-explanatory. So there is no dataChanged() signal necessary.

        saitejS Offline
        saitejS Offline
        saitej
        wrote on last edited by
        #9

        @raven-worx

        Hi

        I didn't get you. If I need to change the existing data, how do i do it with out datachanged() signal

        1 Reply Last reply
        0
        • m.sueM m.sue

          @saitej: you had an addDatamethod, so I suspect you would really add some data.
          @raven-worx: Ah, I see, then they probably emit the \*AboutToBe\* signals, as somehow, of course, the widget must know about these actions.
          -Michael.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #10

          @m.sue said in Dynamic Upadate of data to a ListModel:

          Ah, I see, then they probably emit the *AboutToBe* signals, as somehow, of course, the widget must know about these actions.

          yes, the QAbstractItemView of course does connect to those signals. If you have a custom object/widget, you would also need to connect to those signals.

          @saitej said in Dynamic Upadate of data to a ListModel:

          I didn't get you. If I need to change the existing data, how do i do it with out datachanged() signal

          i said: you only need in the case existing data has changed

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          saitejS 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @m.sue said in Dynamic Upadate of data to a ListModel:

            Ah, I see, then they probably emit the *AboutToBe* signals, as somehow, of course, the widget must know about these actions.

            yes, the QAbstractItemView of course does connect to those signals. If you have a custom object/widget, you would also need to connect to those signals.

            @saitej said in Dynamic Upadate of data to a ListModel:

            I didn't get you. If I need to change the existing data, how do i do it with out datachanged() signal

            i said: you only need in the case existing data has changed

            saitejS Offline
            saitejS Offline
            saitej
            wrote on last edited by
            #11

            @raven-worx

            void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int> ())
            

            Is this used to update the entire model or Can it be used to update a specific item in the listview? If It can be used to update a specific item then what are the arguments (instead of bootrom right)?

            Thanks!!

            raven-worxR 1 Reply Last reply
            0
            • saitejS saitej

              @raven-worx

              void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int> ())
              

              Is this used to update the entire model or Can it be used to update a specific item in the listview? If It can be used to update a specific item then what are the arguments (instead of bootrom right)?

              Thanks!!

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #12

              @saitej
              if it's just a single cell, then topLeft and bottomRight are the same index.
              If you want to update the whole row, then topLeft is QModelIndex(row,0) and bottomRight is QModelIndex(row,colCount-1).
              Also it is possible to set topLeft to QModelIndex(0,0) and bottomRight to QModelIndex(rowCount-1,colCount-1) to update the whole table/list.

              Note that ranges are only supported when topLeft and bottomRight have the same parent index. So as long as you don't use your model in a treeview your are fine.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              saitejS 2 Replies Last reply
              1
              • raven-worxR raven-worx

                @saitej
                if it's just a single cell, then topLeft and bottomRight are the same index.
                If you want to update the whole row, then topLeft is QModelIndex(row,0) and bottomRight is QModelIndex(row,colCount-1).
                Also it is possible to set topLeft to QModelIndex(0,0) and bottomRight to QModelIndex(rowCount-1,colCount-1) to update the whole table/list.

                Note that ranges are only supported when topLeft and bottomRight have the same parent index. So as long as you don't use your model in a treeview your are fine.

                saitejS Offline
                saitejS Offline
                saitej
                wrote on last edited by
                #13

                @raven-worx

                Thanks a lot!!

                1 Reply Last reply
                0
                • raven-worxR raven-worx

                  @saitej
                  if it's just a single cell, then topLeft and bottomRight are the same index.
                  If you want to update the whole row, then topLeft is QModelIndex(row,0) and bottomRight is QModelIndex(row,colCount-1).
                  Also it is possible to set topLeft to QModelIndex(0,0) and bottomRight to QModelIndex(rowCount-1,colCount-1) to update the whole table/list.

                  Note that ranges are only supported when topLeft and bottomRight have the same parent index. So as long as you don't use your model in a treeview your are fine.

                  saitejS Offline
                  saitejS Offline
                  saitej
                  wrote on last edited by saitej
                  #14

                  @raven-worx

                  Hi

                  I am trying to connect a property change signal to datachanged signal through the following code.

                  for(int i=0;i<model.rowCount();i++)
                      {
                           if(model.data(model.index(i),UAVDataModel::NameRole).toString() == "Initial Polygon Area" ){
                               connect(&av1,SIGNAL(initialPolygonAreaChanged(qreal)),&model,SIGNAL(dataChanged(model.index(i),model.index(i))));
                           }
                  
                      }
                  

                  I am getting an error

                  QObject::connect: No such signal DataModel::dataChanged(model.index(i),model.index(i))

                  Note: I have not redeclared the signal in the model as it is inherited from QAbstractListModel

                  raven-worxR 1 Reply Last reply
                  0
                  • saitejS saitej

                    @raven-worx

                    Hi

                    I am trying to connect a property change signal to datachanged signal through the following code.

                    for(int i=0;i<model.rowCount();i++)
                        {
                             if(model.data(model.index(i),UAVDataModel::NameRole).toString() == "Initial Polygon Area" ){
                                 connect(&av1,SIGNAL(initialPolygonAreaChanged(qreal)),&model,SIGNAL(dataChanged(model.index(i),model.index(i))));
                             }
                    
                        }
                    

                    I am getting an error

                    QObject::connect: No such signal DataModel::dataChanged(model.index(i),model.index(i))

                    Note: I have not redeclared the signal in the model as it is inherited from QAbstractListModel

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #15

                    @saitej
                    thats not how signal/slot connections work.
                    Rather you should connect the model's dataChanged() signal to a slot of yours, and in the slot check the passed index if it's the row/column you want.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    saitejS 1 Reply Last reply
                    0
                    • raven-worxR raven-worx

                      @saitej
                      thats not how signal/slot connections work.
                      Rather you should connect the model's dataChanged() signal to a slot of yours, and in the slot check the passed index if it's the row/column you want.

                      saitejS Offline
                      saitejS Offline
                      saitej
                      wrote on last edited by
                      #16

                      @raven-worx

                      I am not editing the data, it is only displaying data from a source. So connecting datachanged signal to a slot does not make sense.

                      we can use connect() to connect 2 signals right?

                      raven-worxR 1 Reply Last reply
                      0
                      • saitejS saitej

                        @raven-worx

                        I am not editing the data, it is only displaying data from a source. So connecting datachanged signal to a slot does not make sense.

                        we can use connect() to connect 2 signals right?

                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #17

                        @saitej said in Dynamic Upadate of data to a ListModel:

                        So connecting datachanged signal to a slot does not make sense.

                        sorry but that doesn't make sense. I think you still don't understand the concept.
                        It doesn't matter if you edit the data or not.

                        The dataChanged() signal notifies the receiver (-> slot) that the data of the given index(es) has changed, and the data needs to be retrieved from the model again.

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        saitejS 1 Reply Last reply
                        0
                        • raven-worxR raven-worx

                          @saitej said in Dynamic Upadate of data to a ListModel:

                          So connecting datachanged signal to a slot does not make sense.

                          sorry but that doesn't make sense. I think you still don't understand the concept.
                          It doesn't matter if you edit the data or not.

                          The dataChanged() signal notifies the receiver (-> slot) that the data of the given index(es) has changed, and the data needs to be retrieved from the model again.

                          saitejS Offline
                          saitejS Offline
                          saitej
                          wrote on last edited by
                          #18

                          @raven-worx

                          I got what datachanged signal does. I would like to explain my problem better. Sorry for the inconvenience.

                          For ex:
                          I have a signal initialPolygonAreaChanged(qreal) emitted when ever initialPolygonArea is changed . I know the index of initialPolygonArea in the model. I need to connect the initialPolygonAreaChanged(qreal) with datachanged() signal. Is this the right way?

                          raven-worxR 1 Reply Last reply
                          0
                          • saitejS saitej

                            @raven-worx

                            I got what datachanged signal does. I would like to explain my problem better. Sorry for the inconvenience.

                            For ex:
                            I have a signal initialPolygonAreaChanged(qreal) emitted when ever initialPolygonArea is changed . I know the index of initialPolygonArea in the model. I need to connect the initialPolygonAreaChanged(qreal) with datachanged() signal. Is this the right way?

                            raven-worxR Offline
                            raven-worxR Offline
                            raven-worx
                            Moderators
                            wrote on last edited by raven-worx
                            #19

                            @saitej said in Dynamic Upadate of data to a ListModel:

                            Is this the right way?

                            so far so good. You can also emit the dataChanged() signal whenever you emit initialPolygonAreaChanged(), since you know the index. (If the initialPolygonAreaChanged() signal is defined in the model)

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            saitejS 1 Reply Last reply
                            0
                            • raven-worxR raven-worx

                              @saitej said in Dynamic Upadate of data to a ListModel:

                              Is this the right way?

                              so far so good. You can also emit the dataChanged() signal whenever you emit initialPolygonAreaChanged(), since you know the index. (If the initialPolygonAreaChanged() signal is defined in the model)

                              saitejS Offline
                              saitejS Offline
                              saitej
                              wrote on last edited by
                              #20

                              @raven-worx

                              It is not defined in the model so I was to trying to connect via this:

                              connect(&av1,SIGNAL(initialPolygonAreaChanged(qreal)),&model,SIGNAL(dataChanged(model.index(i),model.index(i))));

                              What should this be changed to?

                              raven-worxR 1 Reply Last reply
                              0
                              • saitejS saitej

                                @raven-worx

                                It is not defined in the model so I was to trying to connect via this:

                                connect(&av1,SIGNAL(initialPolygonAreaChanged(qreal)),&model,SIGNAL(dataChanged(model.index(i),model.index(i))));

                                What should this be changed to?

                                raven-worxR Offline
                                raven-worxR Offline
                                raven-worx
                                Moderators
                                wrote on last edited by
                                #21

                                @saitej said in Dynamic Upadate of data to a ListModel:

                                connect(&av1,SIGNAL(initialPolygonAreaChanged(qreal)),&model,SIGNAL(dataChanged(model.index(i),model.index(i))));

                                This is not a valid connect statement. You are not allowed/supposed to do do a connection with the values already specified.

                                You will have to do something like this:

                                connect(&av1,SIGNAL(initialPolygonAreaChanged(qreal)),&model,SIGNAL(onInitialPolygonAreaChanged(qreal));
                                

                                In your model's onInitialPolygonAreaChanged(qreal) slot you then emit the dataChanged() signal with the correct indexes.

                                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                If you have a question please use the forum so others can benefit from the solution in the future

                                saitejS 2 Replies Last reply
                                0
                                • raven-worxR raven-worx

                                  @saitej said in Dynamic Upadate of data to a ListModel:

                                  connect(&av1,SIGNAL(initialPolygonAreaChanged(qreal)),&model,SIGNAL(dataChanged(model.index(i),model.index(i))));

                                  This is not a valid connect statement. You are not allowed/supposed to do do a connection with the values already specified.

                                  You will have to do something like this:

                                  connect(&av1,SIGNAL(initialPolygonAreaChanged(qreal)),&model,SIGNAL(onInitialPolygonAreaChanged(qreal));
                                  

                                  In your model's onInitialPolygonAreaChanged(qreal) slot you then emit the dataChanged() signal with the correct indexes.

                                  saitejS Offline
                                  saitejS Offline
                                  saitej
                                  wrote on last edited by
                                  #22

                                  @raven-worx

                                  Oh .. !! I had thought of this but since there were lot of variables I was trying for a different way. I will go ahead this way.
                                  Thanks!!

                                  1 Reply Last reply
                                  0
                                  • raven-worxR raven-worx

                                    @saitej said in Dynamic Upadate of data to a ListModel:

                                    connect(&av1,SIGNAL(initialPolygonAreaChanged(qreal)),&model,SIGNAL(dataChanged(model.index(i),model.index(i))));

                                    This is not a valid connect statement. You are not allowed/supposed to do do a connection with the values already specified.

                                    You will have to do something like this:

                                    connect(&av1,SIGNAL(initialPolygonAreaChanged(qreal)),&model,SIGNAL(onInitialPolygonAreaChanged(qreal));
                                    

                                    In your model's onInitialPolygonAreaChanged(qreal) slot you then emit the dataChanged() signal with the correct indexes.

                                    saitejS Offline
                                    saitejS Offline
                                    saitej
                                    wrote on last edited by
                                    #23

                                    @raven-worx

                                    Mine is a list with a single column with three roles. I need to update only the value role , as suggested I am, emitting datachanged signal via

                                    void DataModel::onInitialPolygonAreaChanged(qreal ){
                                        //0 is the index of IPA
                                        qDebug() << "IPA from model" ;
                                       QVector<int> roleVector(ValueRole);
                                    
                                        emit dataChanged(index(0),index(0),roleVector);
                                    }
                                    

                                    This also does not update the model.

                                    raven-worxR 1 Reply Last reply
                                    0
                                    • saitejS saitej

                                      @raven-worx

                                      Mine is a list with a single column with three roles. I need to update only the value role , as suggested I am, emitting datachanged signal via

                                      void DataModel::onInitialPolygonAreaChanged(qreal ){
                                          //0 is the index of IPA
                                          qDebug() << "IPA from model" ;
                                         QVector<int> roleVector(ValueRole);
                                      
                                          emit dataChanged(index(0),index(0),roleVector);
                                      }
                                      

                                      This also does not update the model.

                                      raven-worxR Offline
                                      raven-worxR Offline
                                      raven-worx
                                      Moderators
                                      wrote on last edited by
                                      #24

                                      @saitej said in Dynamic Upadate of data to a ListModel:

                                      This also does not update the model.

                                      what do you mean?
                                      The dataChanged() signal just informs (the view) about the change. So you need to trigger this signal after the data for this role has changed.
                                      Who exactly is using the ValueRole?

                                      Please show the code of your model.

                                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                      If you have a question please use the forum so others can benefit from the solution in the future

                                      saitejS 1 Reply Last reply
                                      0
                                      • raven-worxR raven-worx

                                        @saitej said in Dynamic Upadate of data to a ListModel:

                                        This also does not update the model.

                                        what do you mean?
                                        The dataChanged() signal just informs (the view) about the change. So you need to trigger this signal after the data for this role has changed.
                                        Who exactly is using the ValueRole?

                                        Please show the code of your model.

                                        saitejS Offline
                                        saitejS Offline
                                        saitej
                                        wrote on last edited by saitej
                                        #25

                                        @raven-worx

                                        I have triggered it exactly when my data is being changed.

                                        My model:

                                        class DataList{
                                        public:
                                            DataList(const QString &name, const QString &value, const QString &category);
                                        
                                            QString name() const;
                                            QString value() const;
                                            QString category() const;
                                        
                                        private:
                                            QString m_name;
                                            QString m_value;
                                            QString m_category;
                                        };
                                        
                                        class DataModel : public QAbstractListModel
                                        {
                                            Q_OBJECT
                                        public:
                                            enum Roles {
                                                NameRole = Qt::UserRole + 1,
                                                ValueRole,
                                                CategoryRole
                                            };
                                        
                                            DataModel(QAbstractItemModel *parent = 0);
                                            virtual ~DataModel() {}
                                            void addData(const DataList &datalist);
                                            int rowCount(const QModelIndex & parent = QModelIndex()) const;
                                        
                                            QVariant data(const QModelIndex & index, int role=NameRole) const;
                                        protected:
                                            QHash<int, QByteArray> roleNames() const;
                                        
                                        private:
                                            QList<DataList> m_datalist;
                                        
                                        public slots:
                                            void onInitialPolygonAreaChanged(qreal);
                                        
                                        
                                        };
                                        
                                        
                                        raven-worxR 1 Reply Last reply
                                        0
                                        • saitejS saitej

                                          @raven-worx

                                          I have triggered it exactly when my data is being changed.

                                          My model:

                                          class DataList{
                                          public:
                                              DataList(const QString &name, const QString &value, const QString &category);
                                          
                                              QString name() const;
                                              QString value() const;
                                              QString category() const;
                                          
                                          private:
                                              QString m_name;
                                              QString m_value;
                                              QString m_category;
                                          };
                                          
                                          class DataModel : public QAbstractListModel
                                          {
                                              Q_OBJECT
                                          public:
                                              enum Roles {
                                                  NameRole = Qt::UserRole + 1,
                                                  ValueRole,
                                                  CategoryRole
                                              };
                                          
                                              DataModel(QAbstractItemModel *parent = 0);
                                              virtual ~DataModel() {}
                                              void addData(const DataList &datalist);
                                              int rowCount(const QModelIndex & parent = QModelIndex()) const;
                                          
                                              QVariant data(const QModelIndex & index, int role=NameRole) const;
                                          protected:
                                              QHash<int, QByteArray> roleNames() const;
                                          
                                          private:
                                              QList<DataList> m_datalist;
                                          
                                          public slots:
                                              void onInitialPolygonAreaChanged(qreal);
                                          
                                          
                                          };
                                          
                                          
                                          raven-worxR Offline
                                          raven-worxR Offline
                                          raven-worx
                                          Moderators
                                          wrote on last edited by
                                          #26

                                          @saitej said in Dynamic Upadate of data to a ListModel:

                                          I have triggered it exactly when my data is being changed.

                                          but who reads the ValueRole?! The view doesn't, it uses the `Qt::DisplayRole' by default.

                                          Also posting the header file of your model implementation is almost as good as providing nothing ;)

                                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                          If you have a question please use the forum so others can benefit from the solution in the future

                                          saitejS 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