Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Use setData() from QML

    QML and Qt Quick
    3
    14
    1866
    Loading More Posts
    • 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.
    • E
      ed_robcont_ @sierdzio last edited by

      @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 Reply Quote 0
      • E
        ed_robcont_ last edited by 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);
        }
        
        sierdzio 1 Reply Last reply Reply Quote 1
        • 6thC
          6thC last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • sierdzio
            sierdzio Moderators @ed_robcont_ last edited by

            @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 Reply Quote 0
            • E
              ed_robcont_ @sierdzio last edited by

              @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()?

              sierdzio 1 Reply Last reply Reply Quote 0
              • sierdzio
                sierdzio Moderators @ed_robcont_ last edited by

                @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 Reply Quote 1
                • E
                  ed_robcont_ @sierdzio last edited by ed_robcont_

                  @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 Reply Quote 0
                  • sierdzio
                    sierdzio Moderators last edited by

                    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 Reply Quote 0
                    • E
                      ed_robcont_ @sierdzio last edited by ed_robcont_

                      @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 Reply Quote 0
                      • E
                        ed_robcont_ last edited by

                        Done, by inserting rows one by one.

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post