Unsolved How to use setData on inserted row in QSqlTableModel
-
I want to insert a new record into a QSqlTableModel with insertRow. How can I use QSqlTableModel::setData() when I use QSqlTableModel::insertRow()?
m_accountModel->insertRow(0);
How can I set the data for the new inserted row. QSqlTableModel::setData needs an index to set the data, but how can I get the index?
Or is there another way to do this?
-
m_accountModel->insertRow(0); QModelIndex *index = m_accountModel->index(0, 0); m_accountModel->setData(index, "Here");
-
@JonB Thanks
How can I achieve that the new inserted data re written to the database?
m_accountModel->setEditStrategy(QSqlTableModel::OnManualSubmit); m_accountModel->insertRow(0); QModelIndex *index = m_accountModel->index(0, 0); m_accountModel->setData(index, "Here"); m_accountModel->submitAll();
This doesn't write the data to the database. What am I doing wrong here?
-
@Infinity
In principle outline looks fine. My guess: check the return from thesetData()
, I only gave example, maybe yours does not accept anything/a string in that column, and (most likely) checksubmitAll()
, if the newly inserted row (which here has only one column initialized if at all) is not acceptable for a row in the database it will get rejected. -
@JonB I found the reason why submitAll() did not write the data to the database table. It was because I didn't set a value for the primary key in the record. Now it works fine.
Thank you very much for your help.
-
@Infinity
Yes,QSqlTableModel
will let you insert into it an incomplete row with columns unset or set to bad value. But then when yousubmit()
the database will barf on theINSERT
statement, which is what you are seeing. It is a good idea to check/listen for all warning output from the db so that you know what is going on, I subclass/set handlers on allQSql...
classes I use for that. -
To add to the previous, I practically dropped use of setData() in favour of insertRecord().
So what I usually do (but my use is never performance oriented):QSqlRecord rec=record(); //returns me empty, well formatted record //here is filling the record with data insertRecord(row,rec); // row is position at which I insert
There is also setRecord() method available. Both took lots of headache from me.
-
@artwaw
+1 for using theQSqlRecord
-level stuff. Nicer thansetData(row, column)
-level.
But the "empty record" is not necessarily acceptable for databaseINSERT
, you still have to fill in any compulsory columns don't you? -
@JonB Depends. In my use cases (again, what works for me doesn't have to work for the others) all I have to take care of are the fields that explicitly can't be null. Indexes are autofilled. As I said, my use cases are quite simple - but then again, QSqlRecord-related stuff has a lot of capabilities I simply don't use.
-
@artwaw said in How to use setData on inserted row in QSqlTableModel:
all I have to take care of are the fields that explicitly can't be null.
Yes. But I think (correct me if wrong) that the
QSqlRecord
-level does not check this for you, it lets you leave and then theINSERT
/UPDATE
db statement errors. That is what I was referring to. -
@JonB No, you are correct. As far as I can tell (I probably did a mistake like this in the past just can't recall) attempt to insert a malformed record, be it without necessary fields filled or with a wrong structure will fail. (it is probably also in the docs but can't dive there at this moment to check)
-
@artwaw
Yes, IIRC in (my beloved) C# data/database handling classes, it stopped you putting an invalid-type-value into itsQRecord
internally before db involved, so this sort of issue was picked up before sending to db, which is nice. Obviously one could write one's own subclass ofQRecord
to achieve this, I think. -
I'm working now with setData instead of QSqlRecord. I found it to be cleaner especially when updating values.