Yes, it works perfectly.
I'm using Qt 4.2.2
Yes, it works perfectly.
I'm using Qt 4.2.2
It's weird that when I calculate the position of the to be inserted row with model->rowCount() and then call insertRow() at that position. It still adds the row at the top of the table. While in your example in inserts the row at the bottom.
Hmm, so maybe it happens only when using QSortFilterProxyModel + QSqlTableModel?
J-P Nurmi
Perhaps... If that would be true, is there a way I can work around this problem that you know of?
Could you, by any chance, prepare a minimal example reproducing the problem we could play with?You could for example modify the earlier example of mine to use an SQLite db or so..
J-P Nurmi
Sure, I'll do that.
I made an example with an in memory sqlite database for you, it illustrates the problem very clearly (in my opinion).
Some steps to reproduce the problem:
1) Sort on country (instead of id)
2) Add a new row
3) Enter for example "Italy" in the textbox
4) Press enter
The view resorts itself, but there's an empty row at the top.
Let me know if I can help you any further.
Thanks for looking into it.
Looks like the edit call causes the malfunction, probably because the "id" field is set to "0" instead of the proper index.
I changed the addRow() method and it works fine now:
Qt Code:
void Table::addRow() { int row = proxyModel->sourceModel()->rowCount(); proxyModel->sourceModel()->insertRow(row); if (idx.isValid()) { proxyModel->sourceModel()->setData(idx.sibling(idx.row(), 0), row); // added here idx = proxyModel->mapFromSource(idx); setCurrentIndex(idx); edit(idx); } }To copy to clipboard, switch view to plain text mode
I'll check it out when I get home, thanks for helping!
It's weird, but your solution which worked for you doesn't work for me.
When I add your line of code to the example program and add a new country the id gets set immediately with a value that's already present in de database. After editing the table doesn't get sorted either.
Are you also using Qt 4.2.2 with sqlite3?
Try replacing the whole slot as I also changed the model calls to reference the source model everywhere. Also try moving the setData call out of the if statement - that was where I placed it at first. There shouldn't be any difference but who knows...
I'm using 4.2.0 here, but that shouldn't matter.Are you also using Qt 4.2.2 with sqlite3?
Last edited by saknopper; 6th February 2007 at 13:53. Reason: spelling error
I tested against Qt4.2.2. Looks like it works if you set an id which is already there (row), but doesn't if you pass a correct one (row+1). Anyway this is just cased by the way the sqltablemodel works. The proxy is just confused because the index changes. Reseting the model should help, but you'll lose selection and position of the view as it will also be reset.
I would have though that setting the column as "auto increment" could have solved the problem but seems not..![]()
J-P Nurmi
Can you provide a minimal compilable example reproducing the problem? I'd like to take a look...
Bookmarks