Qt6 topics merged with General

Qml cannot access tableview cell

  • 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:
    I also Tried this code:
    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.

  • Moderators

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

  • 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


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

    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

  • Moderators

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

Log in to reply