Nominate our 2022 Qt Champions!

How to edit cell row in costume model which inherits from QSqlTableModel ?

  • I want to edit a cell row but I am confused how to do it exactly here is my code:


    bool UserModel::setData(const QModelIndex &index, const QVariant &value, int role){
        if (index.isValid() && role == Qt::EditRole){
            return QSqlTableModel::setData(index,value, role);
        return false;


    TextField {
           id: phone
           text: "8989341"            
    TextField {
           id: address
           text: "London"
    ListView {
                  id: view
                  currentIndex: 0
                  delegate: Text {
                 model: UserModel {  id: user_model }            
    Button {
           id: save_btn
           text: "save"
           background: Rectangle {
                   border.color: "silver"
                   border.width: 2
                    radius: 10
            onClicked: {
                     user_model.setData(user_model.index(1, 2), address.text)
                     user_model.setData(user_model.index(2, 3), phone.text)

    table schema:

    users(u_id int primary key, name varchar, address varchar, phone int)

    The above code does not save the edited data.

  • @Ahti
    Dunno, but why don't you start by showing us/yourself the return values of the calls to user_model.setData(user_model.index(...?

    What's the result for you of: ?