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

Send ListModel from QML to C++



  • HI,

    I need help in saving data to database via C++ from the QML data list.

    In my screen I have 1 combo box for Item selection, then 1 text box for quantity and a add button.

    Add button will add the items to dataModel, i.e. my TableView model

    dataModel.append({"id": cmbProductId.currentText ,"quantity":txtQuantity.text})

    Now the TableView is showing the added data correctly.

    Upon Save, I want to know how to pass the dataModel to my C++ function , and how can i access the values in C++ function.

    I have written an Q_INVOKABLE bool saveData();

    But i am not getting what should be the parameter for this function and how to access the dataModel values in CPP

    Thanks in advance



  • Hi,

    I would create a Model in c++ (inherited from QAbstractListModel)

    Provide an implementation of setData() in the model,

    And expose the model to QML (and to your TableView)

    Info:
    http://qt-project.org/doc/qt-5/qabstractlistmodel.html

    look at the subclass section,



  • Hi,
    Thanks for the solution. I am implementing that.

    By doing that, i need to call setData() eveytime i add an item to list model.

    Is there any way so that after adding all items , say 10 items, I can send the model to C++ function at once?



  • setData is just to update an already existing data row I guess, if you want to add new data you should implement your own function/slot for that I think and you can do what you want in there, if you want add 10 rows at once or whatever. You just have to emit the proper signals so the views knows that you have added some new rows, see here: http://qt-project.org/doc/qt-5/qabstractitemmodel.html#beginInsertRows


Log in to reply