Understand why a row is not submitted
-
wrote on 27 Nov 2024, 07:23 last edited by
I'm using a
QSqlTableModel
(actually aQSqlRelationalTabelModel
) in Qt 6.8.0.
I set theeditStrategy
toOnRowChange
.
On the form I show the data using aQTableView
.It happens that when I insert a new row and press "enter" (or move to another row) the record is not submitted to the database, i.e. the vertical header still show the
*
sign.I guess it is because it cannot be submitted due to a wrong value somewhere. In fact, it does not happens on very simple tables. Right now, I'm not interested to implement a validator (like in this question). I just want to get an error message whenever it cannot save the row.
I searched in the docs about
QSqlTableModel
andQTableView
but I find nothing about. -
I'm using a
QSqlTableModel
(actually aQSqlRelationalTabelModel
) in Qt 6.8.0.
I set theeditStrategy
toOnRowChange
.
On the form I show the data using aQTableView
.It happens that when I insert a new row and press "enter" (or move to another row) the record is not submitted to the database, i.e. the vertical header still show the
*
sign.I guess it is because it cannot be submitted due to a wrong value somewhere. In fact, it does not happens on very simple tables. Right now, I'm not interested to implement a validator (like in this question). I just want to get an error message whenever it cannot save the row.
I searched in the docs about
QSqlTableModel
andQTableView
but I find nothing about.@Mark81 You can get last error using https://doc.qt.io/qt-6/qsqlquerymodel.html#lastError
-
@Mark81 You can get last error using https://doc.qt.io/qt-6/qsqlquerymodel.html#lastError
wrote on 27 Nov 2024, 07:34 last edited by@jsulm said in Understand why a row is not submitted:
@Mark81 You can get last error using https://doc.qt.io/qt-6/qsqlquerymodel.html#lastError
Correct, but when I can check it? It seems it does not emit a signal. So how I can catch the moment to check if it has some errors?
I tried the following:
connect(model, &QSqlRelationalTableModel::beforeInsert, this, [=](QSqlRecord &record) { qDebug() << model->lastError(); });
actually, the lambda is executed when I try to submit the faulty row pressing enter (like using an already existing
id
value) but it returns no errors:QSqlError("", "No Fields to update", "")
-
wrote on 27 Nov 2024, 07:44 last edited by
Ok, I understood how it works. To show the actual error, in the example:
UNIQUE constraint failed: table.id Unable to fetch row
it's not enough to press enter, but I need to click on another row.
From a user perspective it's not correct: if he presses "enter" it should submit or return an error.
Of course using theOnManualSubmit
option would avoid this scenario but it requires to click on aQPushButton
instead of simply press "enter". -
Ok, I understood how it works. To show the actual error, in the example:
UNIQUE constraint failed: table.id Unable to fetch row
it's not enough to press enter, but I need to click on another row.
From a user perspective it's not correct: if he presses "enter" it should submit or return an error.
Of course using theOnManualSubmit
option would avoid this scenario but it requires to click on aQPushButton
instead of simply press "enter".wrote on 27 Nov 2024, 08:31 last edited by JonB@Mark81 said in Understand why a row is not submitted:
it's not enough to press enter, but I need to click on another row.
From a user perspective it's not correct: if he presses "enter" it should submit or return an error.https://doc.qt.io/qt-6/qsqltablemodel.html#EditStrategy-enum
QSqlTableModel::OnRowChange 1 Changes to a row will be applied when the user selects a different row.
Of course using the OnManualSubmit option would avoid this scenario but it requires to click on a QPushButton instead of simply press "enter".
So it sounds like you need to handle pressing Enter on a row/the table yourself. Then you can either select a different row if using
OnRowChange
or callsubmitAll()
if usingOnManualSubmit
. -
@Mark81 said in Understand why a row is not submitted:
it's not enough to press enter, but I need to click on another row.
From a user perspective it's not correct: if he presses "enter" it should submit or return an error.https://doc.qt.io/qt-6/qsqltablemodel.html#EditStrategy-enum
QSqlTableModel::OnRowChange 1 Changes to a row will be applied when the user selects a different row.
Of course using the OnManualSubmit option would avoid this scenario but it requires to click on a QPushButton instead of simply press "enter".
So it sounds like you need to handle pressing Enter on a row/the table yourself. Then you can either select a different row if using
OnRowChange
or callsubmitAll()
if usingOnManualSubmit
.wrote on 27 Nov 2024, 08:37 last edited by@JonB said in Understand why a row is not submitted:
https://doc.qt.io/qt-6/qsqltablemodel.html#EditStrategy-enum
QSqlTableModel::OnRowChange 1 Changes to a row will be applied when the user selects a different row.
That's not actually true.
If you fill a new row correctly and press enter (even with the above edit strategy) the row is submitted immediately without the needs to select a different row. This is not consistent from my point of view. -
@JonB said in Understand why a row is not submitted:
https://doc.qt.io/qt-6/qsqltablemodel.html#EditStrategy-enum
QSqlTableModel::OnRowChange 1 Changes to a row will be applied when the user selects a different row.
That's not actually true.
If you fill a new row correctly and press enter (even with the above edit strategy) the row is submitted immediately without the needs to select a different row. This is not consistent from my point of view.wrote on 27 Nov 2024, 08:54 last edited by@Mark81
I am guessing thatOnRowChange
gets activated onQTableView::currentChanged()
? Or maybe not. I don't know exactly what happens on Enter or why you find it differs between a new row versus an existing row, if that is what it does. You may need to handle the Enter key directly, e.g. https://pohlondrej.com/qtableview-excel-like-editing//
1/7