Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED]How to get the value of a row in TableView using QML?



  • I know the property currentRow, but I want to get the value instead of the index.

    Is there any method?


  • Moderators

    @CoderJeff Since you already have the index or row number get the value from model. Ofcourse the method depends on which model type you are using.



  • @p3c0

    Yes, I tried to do it like what you said. But the model is from C++:

    TableView {
    id: tableView
    ......
    model: myModelCpp.model()
    ......
    }

    The type of myModelCpp is MySqlModel, which inherits QSqlTableModel.

    Do I need to add a get function in C++ code?


  • Moderators

    @CoderJeff Yes add a Q_INVOKABLE get (or any other suitable name) function.



  • @p3c0
    I feel it is another unreasonable design about QML.

    I think whatever and wherever the model is, once it is in QML, it should be converted to a kind of unified type like List or something else, and QML provides an unified interface that is convenient to call.

    In addition, I tried to add the same code in ComboBox:

    ComboBox {
    ......
    model: myModelCpp.model()
    ......
    }

    But it did not work, showing that the model is undefined.

    Actually, either TableView or ComboBox, for us, their model are both a kind of data set, which should be used everywhere. Otherwise, we need to think about too much.


  • Moderators

    @CoderJeff I suspect you are definitely doing something wrong. Personally I have been using C++ models and never faced any major problems. Same C++ model in my case works perfectly with ListView and ComboBox. In case of ComboBox you have to use textRole to populate but it is not related to model undefined.
    Eg:

    ComboBox {
        model: myModel
        textRole: 'title'
    }
    


  • @p3c0
    You are right. My ComboBox code works according to your method.

    QML is like an unearth buried treasure. As a beginner, I have a very long way to explore it.

    Thank you p3c0.


  • Moderators

    @CoderJeff You're Welcome :)
    I would suggest you to start from QML basics to make the roots strong.


Log in to reply