Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to edit cell row in costume model which inherits from QSqlTableModel ?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlmodel-viewqsqltablemodel
2 Posts 2 Posters 403 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ahti
    wrote on 11 Feb 2020, 14:52 last edited by Ahti 2 Nov 2020, 15:18
    #1

    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.

    what is a signature ?? Lol

    J 1 Reply Last reply 11 Feb 2020, 17:17
    0
    • A Ahti
      11 Feb 2020, 14:52

      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.

      J Offline
      J Offline
      JonB
      wrote on 11 Feb 2020, 17:17 last edited by
      #2

      @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 ?

      1 Reply Last reply
      0

      2/2

      11 Feb 2020, 17:17

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved