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

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:

    usermodel.cpp:

    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;
    }
    

    profile.qml:

    TextField {
           id: phone
           text: "8989341"            
    }
    TextField {
           id: address
           text: "London"
    }
    
    ListView {
                  id: view
                  currentIndex: 0
                  delegate: Text {
                          text: model.name
                  } 
                 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: https://doc.qt.io/qt-5/qsqltablemodel.html#editStrategy ?