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.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.6k 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.
  • V Offline
    V Offline
    Verhoher
    wrote on 25 May 2016, 08:58 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?

    P 1 Reply Last reply 25 May 2016, 09:04
    0
    • V Verhoher
      25 May 2016, 08:58

      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?

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 25 May 2016, 09:04 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
      • V Offline
        V Offline
        Verhoher
        wrote on 25 May 2016, 09:37 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?

        P 1 Reply Last reply 25 May 2016, 09:46
        0
        • V Verhoher
          25 May 2016, 09:37

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

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 25 May 2016, 09:46 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

          V 1 Reply Last reply 25 May 2016, 10:05
          1
          • P p3c0
            25 May 2016, 09:46

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

            V Offline
            V Offline
            Verhoher
            wrote on 25 May 2016, 10:05 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 :)

            P 1 Reply Last reply 25 May 2016, 10:10
            0
            • V Verhoher
              25 May 2016, 10:05

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

              P Offline
              P Offline
              p3c0
              Moderators
              wrote on 25 May 2016, 10:10 last edited by
              #6

              @Verhoher You're Welcome. Happy Coding :)

              157

              1 Reply Last reply
              0

              6/6

              25 May 2016, 10:10

              • Login

              • Login or register to search.
              6 out of 6
              • First post
                6/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved