[Qt-interest] QSqlTableModel: How to retrieve a primary key for a new row?

Timo Schmiade the_isz at gmx.de
Wed Mar 3 16:32:43 CET 2010


On 02/03/2010 17:18, Yevgen Yashchenko wrote:
> If you don't want to store id's you can take my approach to the problem:
>
> before adding the row store the previously selected row index somewhere;
> insertRow after the currently selected one;
> get the index's sibling one row down - that sibling will be exactly the
> new added row.
>
> This needs special handling of situations when no row is selected or
> there are no rows at all though.

Hi again, Yevgen,

thanks for your continuous support, but I'm not sure you understand my
problem (granted, maybe I didn't make myself clear enough).

I want the database (SQLITE in my case) to be in charge of choosing the
ID of a newly created row in it. And I also want to have that ID
available as soon as I call QTableModel::insertRow().

And I think that's exactly where my problem lies... QTableModel doesn't
commit the insertion immediately to the database, but caches it, which
is why the database can't create an ID yet. That's my theory so far...

As far as I can see, I'll either have to manage the IDs myself (as you
suggested, Yevgen) or I'll need to commit the insertions immediately and
edit the (empty) entries afterwards.

Please correct me if I'm wrong!



More information about the Qt-interest-old mailing list