user2454413
user2454413

Reputation: 21

updating the selection model and proxy model after insertion into a custom model in qt

I have a model in which i will be adding symbols dynamically. I am using a proxy model and a selection model for linking with the view Every time i add a symbol to my source model the proxy model gets updated but the selection model i set for the view gets screwed up. It doesn't recognize any selections anymore can someone tell me why ??

void SymbolViewer::on_openButton_clicked()
{
    this->selModel = ui->symbolListView->selectionModel();
     ...
}

i set the selection model each time my i click a button to work on the selection.

Upvotes: 2

Views: 1604

Answers (1)

trompa
trompa

Reputation: 2007

Im sure you re trying to access your model data with the indexes given by the selection model. But selection model will return indexes relative to your view's model. And It is the proxy. So to access data of your model you must map it:

e.g.:

Consider you have a signal on current item change:

 connect( p_selectionModel,
    SIGNAL( currentChanged(const QModelIndex &, const QModelIndex &)),
    this,
    SLOT(viewCurrentChanged(const QModelIndex &, const QModelIndex &)));

On your slot:

viewCurrentChanged(const QItemSelection & selected, const QItemSelection & deselected)

selected will be a QModelIndex of your proxy. You could access to data through

selected.data() ..

But if your accessig this way:

your_model->data( selected, role )

Your accessing your model with a proxy index, that will fail. You should do it this way:

your_model->data( proxy_model->mapToSource(selected) , role )

( mapToSource(...) )

If your working with a QSelection, ( as in selectionChanged(const QItemSelection & selected, const QItemSelection & deselected) signal ) use

mapSelectionToSource()

To do the reverse path, use:

QItemSelection QAbstractProxyModel::mapFromSource(const QModelIndex & sourceIndex) const QItemSelection QAbstractProxyModel::mapSelectionFromSource(const QItemSelection & sourceSelection)

And dont set the selection model on that button slot! It makes no sense. It will be always the same.

Upvotes: 2

Related Questions