Question

I have a window which contains a QTreeView (databaseTreeView) on a subclass of QAbstractTableModel (via a QSortFilterProxyModel). I have an [Add] button which calls ui->databaseTreeView->model()->insertRow(ui->databaseTreeView->model()->rowCount());. I also have an [Edit] Button, which uses ui->databaseTreeView->selectionModel()->currentIndex(); to pass an index to an editor window.

I'd like to have the [Add] button automatically open an editor with the index of the newly created item, but as the model is sorted, I can't use the row number where I inserted the row. Plus insertRows() only returns a bool (an index would be far more handy for my situation, so I'm wondering if anyone can tell me how to get the index of the newly added item).

I'm willing to devote two or three hours to it, refactoring if there's no alternative, but I would have thought it should be fairly simple. I'm looking inquisitively at the QAbstractItemModel::rowsInserted signal, but that just returns the rows I gave insertRows(), and edits the last item in the list, regardless of whether it was the item just added.

The full source can be found at https://github.com/megamasha/FlashKard for anyone to take a closer look.

Was it helpful?

Solution 2

Well, it seems I managed to answer my own question - I added my own signal to the model class, and sent it from the addRows() function. I then connected it to a slot in the appropriate window, so it could create an editor as its child.

I'd still be interested to hear suggestions that feel less like a kludgey workaround - I feel like I should be able to use preexisting slots to achieve the same goal.

I shall see if mapToSource() can be of use...

OTHER TIPS

http://doc.qt.nokia.com/4.7/qsortfilterproxymodel.html#details and The QSortFilterProxyModel acts as a wrapper for the original model. "If you need to convert source QModelIndexes to sorted/filtered model indexes or vice versa, use mapToSource(), mapFromSource(), mapSelectionToSource(), and mapSelectionFromSource()."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top