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. Reading data from TableView with C++ model in it.

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.6k Views
  • 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.
  • VerhoherV Offline
    VerhoherV Offline
    Verhoher
    wrote on last edited by
    #1

    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?

    p3c0P 1 Reply Last reply
    0
    • VerhoherV Verhoher

      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?

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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
      1
      • VerhoherV Offline
        VerhoherV Offline
        Verhoher
        wrote on last edited by
        #3

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

        p3c0P 1 Reply Last reply
        0
        • VerhoherV Verhoher

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

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @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

          VerhoherV 1 Reply Last reply
          1
          • p3c0P p3c0

            @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.

            VerhoherV Offline
            VerhoherV Offline
            Verhoher
            wrote on last edited by
            #5

            @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 :)

            p3c0P 1 Reply Last reply
            0
            • VerhoherV Verhoher

              @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 :)

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @Verhoher You're Welcome. Happy Coding :)

              157

              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