Model-View: how to get the data from my selected rows
Code:
QModelIndexList listModelIndex = selectionModel->selectedRows();
for (QModelIndexList::Iterator iter = listModelIndex.begin(); iter != listModelIndex.end (); ++iter, ++pChan) {
index = *iter;
row = index.row();
pChan->m_channel = modelChannel->data(modelChannel->index(row, 0), Qt::DisplayRole).toUInt();
pChan->m_isRecorded = modelChannel->data(modelChannel->index(row, 1), Qt::DisplayRole).toUInt();
pChan->m_isPrioritied = modelChannel->data(modelChannel->index(row, 2), Qt::DisplayRole).toUInt();
}
First I get the list of the selected rows; then I want to get the index of every row. However because the data have been sorted, so the getted row-index in Line L4 is not the one, which I want to get. I want get the index of the selected one in view by my eyes, not the one in the model in computer mem!
Maybe it is not easy to understand only by my above words. So you can try the example of QT4.4.X\examples\itemviews\addressbook. First you input 3 names and addresses one by one, such as <Alice, anywhere>, <Bring, anywhere> and <John, anywhere>, then you try to change the Alice' adress. You will find it can not to be changed!
Re: Model-View: how to get the data from my selected rows
I suggest you apply a sort filter proxy model between your model and the view so that the view sorts using the proxy. You'll then have access to mapping between the source and the proxy models so that you can operate on any of them.
Re: Model-View: how to get the data from my selected rows
Quote:
Originally Posted by
wysota
I suggest you apply a sort filter proxy model between your model and the view so that the view sorts using the proxy. You'll then have access to mapping between the source and the proxy models so that you can operate on any of them.
Yes, does define a proxy class derived from QSortFilterProxyModel. But this cannot make it possible to get the wanted index.
If you try the example, then you can find the real problem!
Re: Model-View: how to get the data from my selected rows
Quote:
Originally Posted by
viasant
But this cannot make it possible to get the wanted index.
Yes, it does. If it does not, then you are doing something wrong elsewhere.
Re: Model-View: how to get the data from my selected rows
Quote:
Originally Posted by
viasant
Yes, does define a proxy class derived from QSortFilterProxyModel. But this cannot make it possible to get the wanted index.
Quite simple actually... use mapToSource and mapFromSource. See
QSortFilterProxyModel documentaion.
If you have several proxys below each other for sorting on more than one parameter, you need to to mapto/mapfrom starting at the lowest level en go through each proxy to get to your sourcemodel, and then get the index you want!
Re: Model-View: how to get the data from my selected rows