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 a cell in a model inheriting from QSqlTableModel

How to edit a cell in a model inheriting from QSqlTableModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlmodel-view
7 Posts 3 Posters 975 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 16 Feb 2020, 04:34 last edited by Ahti
    #1

    I have created a model that stores list of users with their profile information. The users are shown on main.qml and their profile on profile.qml.

    The profile can be access by swiping the user name to right which will bring profile.qml into view. After that the user can edit his/her info by hitting the save button.

    What is the problem:

    When I click on the save button to save the profile info the model seems to get updated but the view doesn't. The view is only updated when I re-run the application once again.

    main.qml:

     // this file displays the list of users from the model
    
    StackView {
        id: home
        anchors.fill: parent
        padding: 0
        property var model: null
    
        initialItem: Pane {
            ColumnLayout {
                    id: body
    
                    ListView {
                        id: view
                        currentIndex: -1
                        height: body.height
                        width: body.width                        
    
                        delegate: SwipeDelegate {
                                id: content
                                width: parent.width
                                height: 50
                                text: model.name
                                swipe.onCompleted: {
                                    if (swipe.position > 0){
                                         home.model = model
                                         home.pop()										 
                                         home.push("Profile.qml")
    				}
                                    swipe.close()
                                }
    
                                swipe.left: Label {
                                    text: qsTr("Profile")
                                    color: "white"
                                    verticalAlignment: Label.AlignVCenter
                                    padding: 12
                                    height: parent.height
                                    anchors.left: parent.left
                                    opacity: 2 * content.swipe.position
                                }
                        }
    
                        model: UserModel { id: user_model }
    
                        ScrollIndicator.vertical: ScrollIndicator {}
                    }
                }
    		}				
    	}
    

    Profile.qml:

    // this file displays the user profile which can be edited and submitted with save button
    
    ColumnLayout {
            id: info
            spacing: 10
    		
            RowLayout {
                id: phone_g
                width: parent.width
    
                TextField {
                    id: phone
                    text: home.model.phone
                    font.pixelSize: 13                    
                }
            }
            RowLayout {
                id: address_g
                width: parent.width
    
                TextField {
                    id: address
                    text: home.model.address
                    font.pixelSize: 13
                }
            }
            RowLayout {
                id: save_l
                width: parent.width
    
                Button {
                    id: save_btn
                    text: "save"
                    onClicked: {
                        console.log("addr: "+user_model.setData(user_model.index(home.model.index, 2), address.text))
                        console.log("phn: "+user_model.setData(user_model.index(home.model.index, 3), phone.text))
                    }
                }
            }
    }	
    

    usermodel.cpp:

    // this is how i update/edit the model
    
    UserModel::UserModel(QObject *parent, QSqlDatabase db) : QSqlTableModel (parent, db)
    {        
    	setTable("users");
    	select();
    	setEditStrategy(QSqlTableModel::OnFieldChange);
    }
    
    bool UserModel::setData(const QModelIndex &index, const QVariant &value, int role){
    	if (index.isValid() && role == Qt::EditRole){
    		bool response = QSqlTableModel::setData(index, value, role);
    		response = QSqlTableModel::submitAll();
    		// tried "emit dataChanged(index, index);" also which doesn't work
    		return response;
    	}
    	return false;
    }
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 16 Feb 2020, 07:45 last edited by
      #2

      Why do you reimplement setData() at all when you don't do anything with the data there? QSqlTableModel does all for you in this case.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 2 Replies Last reply 16 Feb 2020, 12:58
      1
      • C Christian Ehrlicher
        16 Feb 2020, 07:45

        Why do you reimplement setData() at all when you don't do anything with the data there? QSqlTableModel does all for you in this case.

        A Offline
        A Offline
        Ahti
        wrote on 16 Feb 2020, 12:58 last edited by Ahti
        #3

        @Christian-Ehrlicher Right but after hitting save the view doesn't get updated I need to re-run the application to show the change how to overcome this ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 16 Feb 2020, 22:18 last edited by
          #4

          Hi,

          From what I can see you are using the model the same way you would for a widget based application except that in QtQuick things are slightly different.

          Calling setData like you do won't trigger a refresh because the roles used to get the data do not match Qt::EditRole. The writing works because of the default value of the role parameters.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 2 Replies Last reply 17 Feb 2020, 01:27
          1
          • S SGaist
            16 Feb 2020, 22:18

            Hi,

            From what I can see you are using the model the same way you would for a widget based application except that in QtQuick things are slightly different.

            Calling setData like you do won't trigger a refresh because the roles used to get the data do not match Qt::EditRole. The writing works because of the default value of the role parameters.

            A Offline
            A Offline
            Ahti
            wrote on 17 Feb 2020, 01:27 last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • S SGaist
              16 Feb 2020, 22:18

              Hi,

              From what I can see you are using the model the same way you would for a widget based application except that in QtQuick things are slightly different.

              Calling setData like you do won't trigger a refresh because the roles used to get the data do not match Qt::EditRole. The writing works because of the default value of the role parameters.

              A Offline
              A Offline
              Ahti
              wrote on 20 Feb 2020, 14:06 last edited by Ahti
              #6

              @SGaist @Christian-Ehrlicher How would I let the view know about the data change ? And I would like to mention I am displaying the list of users with UserRole like this:

              QVariant UserModel::data(const QModelIndex &index, int role) const{
                  QVariant value;
                  if (index.isValid()) {
                      if (role < Qt::UserRole)
                          value = QSqlQueryModel::data(index, role);
                      else {
                          int columnIdx = role - Qt::UserRole - 1;
                          QModelIndex modelIndex = this->index(index.row(), columnIdx);
                          value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
                      }
                  }
                  return value;
              }
              
              QHash<int, QByteArray> UserModel::roleNames() const{
                  QHash<int, QByteArray> roles;
                  for (int i = 0; i < record().count(); i ++) {
                      roles.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
                  }
                  return roles;
              }
              
              1 Reply Last reply
              0
              • C Christian Ehrlicher
                16 Feb 2020, 07:45

                Why do you reimplement setData() at all when you don't do anything with the data there? QSqlTableModel does all for you in this case.

                A Offline
                A Offline
                Ahti
                wrote on 20 Feb 2020, 17:01 last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0

                1/7

                16 Feb 2020, 04:34

                • Login

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