PDA

View Full Version : Model-View: how to get the data from my selected rows



viasant
6th August 2008, 17:54
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!

wysota
6th August 2008, 18:48
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.

viasant
7th August 2008, 00:05
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!

wysota
7th August 2008, 09:02
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.

luf
7th August 2008, 13:34
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!

yleesun
11th August 2008, 02:16
有没有用过表的关联?