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()?


    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?

  • @Infinity

    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?

    QModelIndex *index = m_accountModel->index(0, 0);
    m_accountModel->setData(index, "Here");

    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 the setData(), I only gave example, maybe yours does not accept anything/a string in that column, and (most likely) check submitAll(), 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 you submit() the database will barf on the INSERT 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 all QSql... 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 the QSqlRecord-level stuff. Nicer than setData(row, column)-level.
    But the "empty record" is not necessarily acceptable for database INSERT, 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 the INSERT/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 its QRecord 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 of QRecord to achieve this, I think.

  • I'm working now with setData instead of QSqlRecord. I found it to be cleaner especially when updating values.

Log in to reply