Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I update a row in the model and commit to database?
Forum Updated to NodeBB v4.3 + New Features

How do I update a row in the model and commit to database?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 206 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.
  • J Offline
    J Offline
    jdent
    wrote on last edited by
    #1

    I want to change a column of a QSqlRecord in a model like this:

    	QSqlRecord rec = model->record(0);
    	auto val = rec.value(1).toString();
    	val += "###";
    	rec.setValue(1, val);
    	bool ok = model->submitAll();
    

    Is this the correct way?

    J 1 Reply Last reply
    0
    • J jdent

      I want to change a column of a QSqlRecord in a model like this:

      	QSqlRecord rec = model->record(0);
      	auto val = rec.value(1).toString();
      	val += "###";
      	rec.setValue(1, val);
      	bool ok = model->submitAll();
      

      Is this the correct way?

      J Offline
      J Offline
      jdent
      wrote on last edited by
      #2

      @jdent No, to affect the model we need to go via the model!

      this is the solution:

      	QModelIndex index = model->index(0, 1);
      	QString v = model->data(index).toString();
      	QModelIndex idIndex = index.siblingAtColumn(0);
      	int id = model->data(idIndex).toInt();
      	v += "???";
      	bool ok = model->setData(index, v); // this is required!!!
      	ok = model->submit();
      
      JonBJ 1 Reply Last reply
      0
      • J jdent has marked this topic as solved on
      • J jdent

        @jdent No, to affect the model we need to go via the model!

        this is the solution:

        	QModelIndex index = model->index(0, 1);
        	QString v = model->data(index).toString();
        	QModelIndex idIndex = index.siblingAtColumn(0);
        	int id = model->data(idIndex).toInt();
        	v += "???";
        	bool ok = model->setData(index, v); // this is required!!!
        	ok = model->submit();
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @jdent
        You do not need to drop down to accessing the model directly via QModelIndex and setData(). You can do it via the QSqlRecord. After you went rec.setValue(1, val); you need to call bool QSqlTableModel::setRecord(int row, const QSqlRecord &values) to transfer the altered in-memory rec to the model:

            QSqlRecord rec = tm.record(0);
            QModelIndex idx = tm.index(0, 3);
            qDebug() << rec.value(3).toString();
            qDebug() << idx.data().toString();
            auto val = rec.value(3).toString();
            val += "###";
            rec.setValue(3, val);
            qDebug() << rec.value(3).toString();
            qDebug() << idx.data().toString();
            tm.setRecord(0, rec);
            qDebug() << rec.value(3).toString();
            qDebug() << idx.data().toString();
        

        This shows the underlying model altered after the tm.setRecord(0, rec); statement. You can then commit() or whatever if you wish.

        record() generates a copy of what is in a model row. setRecord() is required if you alter that and want to write it back to the model row.

        1 Reply Last reply
        0

        • Login

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