Qt Forum

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

    Qt Academy Launch in California!

    Solved Reading data from TableView with C++ model in it.

    General and Desktop
    2
    6
    1285
    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.
    • Verhoher
      Verhoher last edited by

      Hello,

      I'm trying to read data in a specific, selected tableView row to pass it further. The view builds as:

                  TableView {
                      id:tblUsersAdventures
                      anchors.fill: parent
                      model: mainUserHandle.usersAdventuresTable
                      TableViewColumn {
                          id: colAdventure
                          role: "name"
                          title: "Adventure Name"
                          width: rctUsersAdventures.width * 0.9
                      }
                      TableViewColumn {
                          id: colAward
                          role: "award"
                          title: "Award"
                          width: rctUsersAdventures.width * 0.099
                      }
                      onClicked: {
                          console.log("clicked to see adventure " + currentRow)
                          console.log("model.get(currentRow).name " + model.get(currentRow).name)   
      }      
      

      and all data shows correctly from

      mainUserHandle.usersAdventuresTable
      

      which is a QList<QObject *> of:

      class AdventureOnUserData : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(int adventureId READ adventureId WRITE setAdventureId NOTIFY adventureIdChanged)
          Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
          Q_PROPERTY(int award READ award WRITE setAward NOTIFY awardChanged)
          Q_PROPERTY(int status READ status WRITE setStatus NOTIFY statusChanged)
          Q_PROPERTY(QString desc READ desc WRITE setDesc NOTIFY descChanged)
          Q_PROPERTY(QString clue READ clue WRITE setClue NOTIFY clueChanged)
      
      public:
      ...
      

      When I try to access the data in the model with

                          console.log("model.get(currentRow).name " + model.get(currentRow).name)   
      

      I get an error as:

      TypeError: Property 'get' of object AdventureOnUserData(0x1c44c2a06a0),AdventureOnUserData(0x1c44c2a0ea0),AdventureOnUserData(0x1c44c2a15a0),AdventureOnUserData(0x1c44c2a0e20),AdventureOnUserData(0x1c44c2a11a0),AdventureOnUserData(0x1c44c29fda0),AdventureOnUserData(0x1c44c2a14a0) is not a function
      

      How should I fetch the selected data here?

      p3c0 1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @Verhoher last edited by

        Hi @Verhoher,
        The error says it all. Are you sure your class AdventureOnUserData contains get function ? I think you have mistaken it w.r.t ListModel functions ?

        157

        1 Reply Last reply Reply Quote 1
        • Verhoher
          Verhoher last edited by

          @p3c0 said:

          The error says it all. Are you sure your class AdventureOnUserData contains get function ?

          Yes seems that I have misunderstood something. so a way to resolve this would be add something like

          Q_INVOKABLE QVariant get(int p_index)
          {
              return QVariant::fromValue<usersAdventuresTable*>((l_userAdventureTable*)this->at(p_index));
          }
          

          to mainUserHandle and call that in the onClicked? Will this work?

          p3c0 1 Reply Last reply Reply Quote 0
          • p3c0
            p3c0 Moderators @Verhoher last edited by

            @Verhoher
            And where are you going to add this get method ? I think what you are trying to use is QObjectList-based model.
            You will find an example there which explains how to access the data from model. Particularly this line:
            color: model.modelData.color

            The properties of the object are not replicated in the model object, as they are easily available via the modelData object.

            157

            Verhoher 1 Reply Last reply Reply Quote 1
            • Verhoher
              Verhoher @p3c0 last edited by

              @p3c0 said:

              I think what you are trying to use is QObjectList-based model.

              Yes, you are right initially I was trying to do it by that example, but didn't manage to fully apply it for my scenario because I couldn't get

                  ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
              

              to work, so I added the model as a QProperty as QList<QObject *> under a QObject I use in my App, and access it through it.

              and yeah it turns out I can just use

              model[currentRow].name
              

              to access it straight in QML, so the easiest solution. This probably was obvious, but I'm new to QML and didn't think of it.

              Thanks for the help :)

              p3c0 1 Reply Last reply Reply Quote 0
              • p3c0
                p3c0 Moderators @Verhoher last edited by

                @Verhoher You're Welcome. Happy Coding :)

                157

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