Important: Please read the Qt Code of Conduct -

QSqlTableModel signal on insert row submit

  • I am subclassing QSqlTableModel (QSqlRelationalModel to be presize), editStrategy is OnFieldChange, dbdriver is QSQLITE. Table structure has some NOT NULL fields. Also I have QTableView and 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 NULL fields aren't defined), when i define all NOT 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 catch QSqlTableModel::submit() method and its return value: it returns true 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?

  • @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

  • @Kot-Shrodingera
    In that case, I see only 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 to monitor the actual INSERT 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....

  • @JonB
    insertRowIntoTable is 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

  • 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

  • @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 overridden 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?

  • @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 as NOT 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

  • @VRonin
    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").

    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 :)