QSqlTableModel signal on insert row submit
-
wrote on 24 Oct 2018, 08:33 last edited by Kot Shrodingera
I am subclassing
QSqlTableModel
(QSqlRelationalModel
to be presize),editStrategy
isOnFieldChange
, dbdriver isQSQLITE
. Table structure has someNOT NULL
fields. Also I haveQTableView
and button that callsmodel->insertRows(model->rowCount())
. When it is clicked, new row is inserted in view with asterisk(*
) instead of row number (as I understand it, becauseNOT NULL
fields aren't defined), when i define allNOT NULL
fields 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::rowsInserted
is called when empty row is added, so it's not suitable. I tried to catchQSqlTableModel::submit()
method and its return value: it returnstrue
when 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? -
I am subclassing
QSqlTableModel
(QSqlRelationalModel
to be presize),editStrategy
isOnFieldChange
, dbdriver isQSQLITE
. Table structure has someNOT NULL
fields. Also I haveQTableView
and button that callsmodel->insertRows(model->rowCount())
. When it is clicked, new row is inserted in view with asterisk(*
) instead of row number (as I understand it, becauseNOT NULL
fields aren't defined), when i define allNOT NULL
fields 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::rowsInserted
is called when empty row is added, so it's not suitable. I tried to catchQSqlTableModel::submit()
method and its return value: it returnstrue
when 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?wrote on 24 Oct 2018, 09:03 last edited by@Kot-Shrodingera
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 ? -
@Kot-Shrodingera
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 ?wrote on 24 Oct 2018, 09:15 last edited by Kot Shrodingera@JonB
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 -
@JonB
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 numberwrote on 24 Oct 2018, 09:47 last edited by@Kot-Shrodingera
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
QSqlTableModel
so that I can see what is actually happening when. Use the low-level,virtual protected
function http://doc.qt.io/qt-5/qsqltablemodel.html#insertRowIntoTable to monitor the actualINSERT
statement 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....
-
@Kot-Shrodingera
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
QSqlTableModel
so that I can see what is actually happening when. Use the low-level,virtual protected
function http://doc.qt.io/qt-5/qsqltablemodel.html#insertRowIntoTable to monitor the actualINSERT
statement 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....
wrote on 24 Oct 2018, 10:17 last edited by@JonB
insertRowIntoTable
is called even on empty row insert, but returnsfalse
. When field are set and Enter is pressed, method is called again and returnstrue
. I think, I can work with this, thank you very much -
wrote on 24 Oct 2018, 10:47 last edited by VRonin
You can use the InsertProxyModel of this library to achieve the extra row insertion. You can find 2 examples using 2 different ways of submitting your row here
-
You can use the InsertProxyModel of this library to achieve the extra row insertion. You can find 2 examples using 2 different ways of submitting your row here
wrote on 24 Oct 2018, 13:13 last edited by@VRonin
As you know, I am always very respectful of the work you put in... :)In this case: since I personally have sub-classed
QSqlTableModel
and overriddeninsertRowIntoTable()
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? -
@VRonin
As you know, I am always very respectful of the work you put in... :)In this case: since I personally have sub-classed
QSqlTableModel
and overriddeninsertRowIntoTable()
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?wrote on 24 Oct 2018, 13:19 last edited by@JonB said in QSqlTableModel signal on insert row submit:
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 asNOT NULL
before 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
-
@JonB said in QSqlTableModel signal on insert row submit:
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 asNOT NULL
before 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
wrote on 24 Oct 2018, 13:28 last edited by JonB@VRonin
Ah. I believe the user described the interface he gets currently (with hisQTableView
). 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").My overriding
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 :)
7/9