QSqlTableModel signal on insert row submit
I am subclassing
QSqlRelationalModelto be presize),
OnFieldChange, dbdriver is
QSQLITE. Table structure has some
NOT NULLfields. Also I have
QTableViewand button that calls
model->insertRows(model->rowCount()). When it is clicked, new row is inserted in view with asterisk(
*) instead of row number (as I understand it, because
NOT NULLfields aren't defined), when i define all
NOT NULLfields and press Enter, asterisk changing to valid row number, so record is succesfully inserted into database. I want to have signal when this is happening.
QAbstractTableModel::rowsInsertedis called when empty row is added, so it's not suitable. I tried to catch
QSqlTableModel::submit()method and its return value: it returns
truewhen added row submited, which is good, but also when I click on eny existing cell even without editing it. How can I catch the exactly moment of submiting added row?
In your case, what about signals http://doc.qt.io/qt-5/qsqltablemodel.html#primeInsert or inherited http://doc.qt.io/qt-5/qabstractitemmodel.html#rowsInserted ?
As I wrote, these signals are emmited when empty row inserted in model, but not submited to database. I want signal that emmits when all nesessary data for new row is entered , i. e. when asterisk changed to row number
In that case, I see only http://doc.qt.io/qt-5/qsqltablemodel.html#submit signal triggered. So your logic for that needs to distinguish the "new insert" from the "existing update" case, in some shape or form.
If that proves difficult: in my own code (I use MySQL, not SQLite) I subclass and override all necessary methods of
QSqlTableModelso that I can see what is actually happening when. Use the low-level,
virtual protectedfunction http://doc.qt.io/qt-5/qsqltablemodel.html#insertRowIntoTable to monitor the actual
INSERTstatement to the database?
Also, if it were me, I would want to know whether your "submit returns true when I click on eny existing cell even without editing it" is actually submitting a SQL query to the database or not, because if it is that's not good....
insertRowIntoTableis called even on empty row insert, but returns
false. When field are set and Enter is pressed, method is called again and returns
true. I think, I can work with this, thank you very much
As you know, I am always very respectful of the work you put in... :)
In this case: since I personally have sub-classed
insertRowIntoTable()with about 4 lines of code (and other methods), and it successfully does all that is needed for monitoring/knowing when a row is inserted, would you care to summarise in a couple of lines what your code achieves over that?
would you care to summarise in a couple of lines what your code achieves over that?
I might well have misunderstood the question.
What I thought the need is, is for the view showing an extra row where the user could enter at least the data for the columns marked as
NOT NULLbefore the row gets actually pushed to the SQL table.
My library has a proxy model that adds that extra row and let's you decide when a row becomes "acceptable" to be submitted to the main model
Ah. I believe the user described the interface he gets currently (with his
QTableView). Whether he likes it or not is a different matter.
Then his question was: how does he get a notification when, having created a new row, it is actually sent to the database for
INSERT, given that there is no signal for this in his case ("editStrategy is OnFieldChange").
QSqlTableModel::insertRowIntoTable()is how to achieve that. So yours is to do with the interface for adding a new row, right? Whether he would benefit/prefer to change over to that to I cannot say :)