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. Use setData() from QML
Forum Updated to NodeBB v4.3 + New Features

Use setData() from QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
14 Posts 3 Posters 4.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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #2

    ... and your problem is? Your code looks OK. If the delegate does not see the data (since it's in another file), you can try model.channel instead of channel.

    (Z(:^

    E 1 Reply Last reply
    0
    • sierdzioS sierdzio

      ... and your problem is? Your code looks OK. If the delegate does not see the data (since it's in another file), you can try model.channel instead of channel.

      E Offline
      E Offline
      ed_robcont_
      wrote on last edited by ed_robcont_
      #3

      @sierdzio
      First of all, thanks for your quick response.
      The problem is that I don't know how to use setData, index, or others model-related functions from QML. When I try console.log(model.channel) I alwasy get undefined.

      My final goal is to check/uncheck all the delegates' checkboxes when the header checkbox has been checked. My idea was somethig similar

      onHeaderPlotChecked: {
          console.log("check all " + checked);
         
          for (var i = 0; i < model.rowCounts(); < i++) {
              var index = model.index(i, 1); // get row
              model.setData(index, checked, model.PlottedRole); // replace data
          }
      }
      
      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #4

        The standard approach in QML is to not use data() and setData(). You should use model.roleName instead - bot for reading and writing.

        If you have to use setData(), this should work:

        modelId.setData(modelId.index(row, column), value, role)
        

        You need to create the index from the model. Using just integer won't work.

        (Z(:^

        E 1 Reply Last reply
        1
        • sierdzioS sierdzio

          The standard approach in QML is to not use data() and setData(). You should use model.roleName instead - bot for reading and writing.

          If you have to use setData(), this should work:

          modelId.setData(modelId.index(row, column), value, role)
          

          You need to create the index from the model. Using just integer won't work.

          E Offline
          E Offline
          ed_robcont_
          wrote on last edited by
          #5

          @sierdzio said in Use setData() from QML:

          The standard approach in QML is to not use data() and setData(). You should use model.roleName instead - bot for reading and writing.

          Ok, got it. But something is not working and I don't understand where and why.
          When I read data using model.plotted or similar, I always get undefined.
          Instead, when I try to replace some data, e.g. with model.plotted = true, the function setData() is never entered (I observed that on debug).

          If you have to use setData(), this should work:

          modelId.setData(modelId.index(row, column), value, role)
          

          You need to create the index from the model. Using just integer won't work.

          I've tried also this approach. In my previous response I used var index = model.index(i, 1) to iterate through indexes, but without success.

          May be a problem that my DataDelegate is in another QML file?

          1 Reply Last reply
          0
          • E Offline
            E Offline
            ed_robcont_
            wrote on last edited by ed_robcont_
            #6

            DONE!!
            I was picking an invalid index using 1 instead of 0 for column. Also the role values was wrong.
            Here below the "solution".

            // Read
            dmodel.data(dmodel.index(n, 0), DataModel.PlottedRole)
            
            // Write: 
            dmodel.setData(dmodel.index(n, 0), checked, DataModel.PlottedRole)
            

            Last question. By using index(row, column) is the only way to access to the n item of the list? Or I should implement my "custom getter" like this?

            DataItem DataModel::get(int row)
            {
                return mList.at(row);
            }
            
            sierdzioS 1 Reply Last reply
            1
            • 6thC6 Offline
              6thC6 Offline
              6thC
              wrote on last edited by
              #7
              This post is deleted!
              1 Reply Last reply
              0
              • E ed_robcont_

                DONE!!
                I was picking an invalid index using 1 instead of 0 for column. Also the role values was wrong.
                Here below the "solution".

                // Read
                dmodel.data(dmodel.index(n, 0), DataModel.PlottedRole)
                
                // Write: 
                dmodel.setData(dmodel.index(n, 0), checked, DataModel.PlottedRole)
                

                Last question. By using index(row, column) is the only way to access to the n item of the list? Or I should implement my "custom getter" like this?

                DataItem DataModel::get(int row)
                {
                    return mList.at(row);
                }
                
                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #8

                @ed_robcont_ said in Use setData() from QML:

                DONE!!
                I was picking an invalid index using 1 instead of 0 for column. Also the role values was wrong.

                Thanks for posting.

                DataItem DataModel::get(int row)
                {
                    return mList.at(row);
                }
                

                Since you're not using the standard approach anyway, I'd say it's up to you. A simple getter like this makes absolute sense.

                (Z(:^

                E 1 Reply Last reply
                0
                • sierdzioS sierdzio

                  @ed_robcont_ said in Use setData() from QML:

                  DONE!!
                  I was picking an invalid index using 1 instead of 0 for column. Also the role values was wrong.

                  Thanks for posting.

                  DataItem DataModel::get(int row)
                  {
                      return mList.at(row);
                  }
                  

                  Since you're not using the standard approach anyway, I'd say it's up to you. A simple getter like this makes absolute sense.

                  E Offline
                  E Offline
                  ed_robcont_
                  wrote on last edited by
                  #9

                  @sierdzio

                  It's working, so it's ok for me. Anyway, I'm still not understanding how the standard approach works.

                  Task: Modify the temperature field in row 3 when a signal is emitted from another QML component.
                  How to access this field in the standard way, without using setData()?

                  sierdzioS 1 Reply Last reply
                  0
                  • E ed_robcont_

                    @sierdzio

                    It's working, so it's ok for me. Anyway, I'm still not understanding how the standard approach works.

                    Task: Modify the temperature field in row 3 when a signal is emitted from another QML component.
                    How to access this field in the standard way, without using setData()?

                    sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #10

                    @ed_robcont_ said in Use setData() from QML:

                    Task: Modify the temperature field in row 3 when a signal is emitted from another QML component.
                    How to access this field in the standard way, without using setData()?

                    Signal emitted from QML should go to your model. Then model should emit dataChanged() and this will update all affected delegates on QML side.

                    (Z(:^

                    E 1 Reply Last reply
                    1
                    • sierdzioS sierdzio

                      @ed_robcont_ said in Use setData() from QML:

                      Task: Modify the temperature field in row 3 when a signal is emitted from another QML component.
                      How to access this field in the standard way, without using setData()?

                      Signal emitted from QML should go to your model. Then model should emit dataChanged() and this will update all affected delegates on QML side.

                      E Offline
                      E Offline
                      ed_robcont_
                      wrote on last edited by ed_robcont_
                      #11

                      @sierdzio said in Use setData() from QML:

                      Signal emitted from QML should go to your model. Then model should emit dataChanged() and this will update all affected delegates on QML side.

                      Ok, it works, thank you!

                      One more thing, it'll be the last I promise.
                      Actually, DataModel is the owner of my list. What I want is to add it from another class, i.e. DataServer. I tried this code but is not working. Any suggestion?

                      @DataServer.cpp

                      void DataServer::showList()
                      {
                          QVector<DataItem*> list;
                          list.append(new DataItem(...);
                          list.append(new DataItem(...);
                          list.append(new DataItem(...);
                      
                          emit listChanged(QVariant::fromValue(list));
                      }
                      

                      @DataModel

                      void DataModel::setList(QVariant list)
                      {
                          m_list = list;
                      }
                      

                      @QML

                      Button {
                          id: btn
                          onPressed: dserver.showList()
                      }
                      
                      DataServer {
                          id: dserver
                          onListChanged: dmodel.setList(list)
                      }
                      
                      1 Reply Last reply
                      0
                      • sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on last edited by
                        #12

                        void DataServer::setList(QVariant list)
                        {
                        m_list = list;
                        }

                        You don't have emit dataChanged() here. Or, in this case, you should probably do:

                        beginResetModel();
                        m_list = list;
                        endResetModel();
                        

                        (Z(:^

                        E 1 Reply Last reply
                        0
                        • sierdzioS sierdzio

                          void DataServer::setList(QVariant list)
                          {
                          m_list = list;
                          }

                          You don't have emit dataChanged() here. Or, in this case, you should probably do:

                          beginResetModel();
                          m_list = list;
                          endResetModel();
                          
                          E Offline
                          E Offline
                          ed_robcont_
                          wrote on last edited by ed_robcont_
                          #13

                          @sierdzio said in Use setData() from QML:

                          You don't have emit dataChanged() here. Or, in this case, you should probably do:

                          beginResetModel();
                          m_list = list;
                          endResetModel();
                          

                          Yes. No signal dataChanged() should be emitted.
                          I did the same you suggested, but I've getting this error.

                          qrc:/main.qml:21: Error: Unknown method parameter type: QVector<DataItem*>
                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            ed_robcont_
                            wrote on last edited by
                            #14

                            Done, by inserting rows one by one.

                            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