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. Qml cannot access tableview cell

Qml cannot access tableview cell

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 2.9k 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.
  • I Offline
    I Offline
    iMasoud
    wrote on last edited by
    #1

    Hi everyone,

    I made a little app which contains cpp code that makes a data model and shares it with Qml using a QProperty . The model works well and i can see all of those data in my Qml tableview.
    But i cannot access to cell contents of that tableview. When i try to do that, I face with this error: "TypeError: Cannot read property 'two' of undefined". (the role "two" exists in model)
    This is my Qml Code:
    console.log(parseInt(myModel_2_1.data(table.currentRow).two));
    I also Tried this code:
    console.log(parseInt(myModel_2_1.data(0).two));
    and i also tried to remove parseInt() and a lot of other things but i didn't made it to access tableview cells content.

    I hope one of you guys help me in this. Thank you all and sorry for my bad English.

    p3c0P 1 Reply Last reply
    0
    • I iMasoud

      Hi everyone,

      I made a little app which contains cpp code that makes a data model and shares it with Qml using a QProperty . The model works well and i can see all of those data in my Qml tableview.
      But i cannot access to cell contents of that tableview. When i try to do that, I face with this error: "TypeError: Cannot read property 'two' of undefined". (the role "two" exists in model)
      This is my Qml Code:
      console.log(parseInt(myModel_2_1.data(table.currentRow).two));
      I also Tried this code:
      console.log(parseInt(myModel_2_1.data(0).two));
      and i also tried to remove parseInt() and a lot of other things but i didn't made it to access tableview cells content.

      I hope one of you guys help me in this. Thank you all and sorry for my bad English.

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

      Hi @iMasoud
      I'm assuming the data method you are referring to is the re-implemented inbuilt method as described here. If so, then you are missing one of the parameter. The correct call should be something like:

      tableModel.data(tableModel.index(0,0), 257); 
      //1st argument = create QModelIndex with row=0 and column=0
      //2nd argument = integer equivalent of rolename as required by the method
      

      Now the problem with this is its not very user friendly to pass an integer as a role. So instead you can write a Q_INVOKABLE method in your model which can return the integer equivalent of your role name. Something like:

      int TableModel::getRoleKey(QString rolename)
      {
          return roleNames().key(rolename.toLatin1());
      }
      

      now substitute this with the data method call in QML

      tableModel.data(tableModel.index(0,0), tableModel.getRoleKey("name")); 
      //add your rolename instead of "name".
      

      Hope this helps...

      157

      1 Reply Last reply
      3
      • I Offline
        I Offline
        iMasoud
        wrote on last edited by
        #3

        Hi @p3c0 ,

        thankyou very much for your reply! I use the arguments that you mentioned but still it didn't work. so i tried this:

        console.log(myModel_2_1.data(myModel_2_1.index(0,0), 2));
        //myModel_2_1 is a QProperty that contains data model provided by c++

        and here is what console printed:
        qml: undefined

        also

        console.log(typeof(myModel_2_1.data(myModel_2_1.index(0,0), 2)));

        that returned "qml: undefined" again!

        i really don't know about data model keys that's been used in myModel_2_1 since the c++ is written by my colleague but this is the c++ function that provides the data model:

        ...
        ...
        ...
        while ( q.next() )
        {
        // QVariant j = QVariant::fromValue(q.at()+1);
        QStandardItem* it = new QStandardItem();
        it->setData(q.at()+1, MyModel::role1); //row number
        it->setData(q.value(0), MyModel::role2); //id
        it->setData(q.value(1), MyModel::role3); //name
        it->setData(q.value(2), MyModel::role4); //parentId
        it->setData(q.value(3), MyModel::role5); //saving
        it->setData(q.value(4), MyModel::role6); //Calendar//////////////////
        it->setData(q.value(5), MyModel::role7); //amount
        it->setData(q.value(6), MyModel::role8); //remaining
        it->setData(q.value(7), MyModel::role9); //Installments
        QString s;
        if(q.value(8).toString() != NULL)
        {
        s=dateShamsi.ToShamsi(QVariant::fromValue(q.value(8).toDate().year()).toString(),
        QVariant::fromValue(q.value(8).toDate().month()).toString(),
        QVariant::fromValue(q.value(8).toDate().day()).toString());
        }
        else { s = ""; }
        it->setData(s, MyModel::role10); //start
        it->setData(q.value(9), MyModel::role11); //Installments_counter
        it->setData(q.value(10), MyModel::role12); //Installments_pass
        my_model->appendRow(it);
        }
        ...
        ...
        ....

        I hope it help you to find out what's wrong. I am really confused with this and there is a deadline that i should make.

        thankyou again and sorry for my bad English

        p3c0P 1 Reply Last reply
        0
        • I iMasoud

          Hi @p3c0 ,

          thankyou very much for your reply! I use the arguments that you mentioned but still it didn't work. so i tried this:

          console.log(myModel_2_1.data(myModel_2_1.index(0,0), 2));
          //myModel_2_1 is a QProperty that contains data model provided by c++

          and here is what console printed:
          qml: undefined

          also

          console.log(typeof(myModel_2_1.data(myModel_2_1.index(0,0), 2)));

          that returned "qml: undefined" again!

          i really don't know about data model keys that's been used in myModel_2_1 since the c++ is written by my colleague but this is the c++ function that provides the data model:

          ...
          ...
          ...
          while ( q.next() )
          {
          // QVariant j = QVariant::fromValue(q.at()+1);
          QStandardItem* it = new QStandardItem();
          it->setData(q.at()+1, MyModel::role1); //row number
          it->setData(q.value(0), MyModel::role2); //id
          it->setData(q.value(1), MyModel::role3); //name
          it->setData(q.value(2), MyModel::role4); //parentId
          it->setData(q.value(3), MyModel::role5); //saving
          it->setData(q.value(4), MyModel::role6); //Calendar//////////////////
          it->setData(q.value(5), MyModel::role7); //amount
          it->setData(q.value(6), MyModel::role8); //remaining
          it->setData(q.value(7), MyModel::role9); //Installments
          QString s;
          if(q.value(8).toString() != NULL)
          {
          s=dateShamsi.ToShamsi(QVariant::fromValue(q.value(8).toDate().year()).toString(),
          QVariant::fromValue(q.value(8).toDate().month()).toString(),
          QVariant::fromValue(q.value(8).toDate().day()).toString());
          }
          else { s = ""; }
          it->setData(s, MyModel::role10); //start
          it->setData(q.value(9), MyModel::role11); //Installments_counter
          it->setData(q.value(10), MyModel::role12); //Installments_pass
          my_model->appendRow(it);
          }
          ...
          ...
          ....

          I hope it help you to find out what's wrong. I am really confused with this and there is a deadline that i should make.

          thankyou again and sorry for my bad English

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

          @iMasoud Check the role. Use the method that I posted earlier viz. getRoleKey.

          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