PDA

View Full Version : selection in filterproxymodel



gyre
4th December 2007, 19:56
Hi I have a question about how can I get selected rows indexes from QSortFilterProxyModel...(selection behaviour is set to select rows)
in QTableView there is a possibility using this:
msgTableView->selectionModel()->selectedRows()

But when I have to models from which one is filtermodel and it has set second one using
filterModel->setSourceModel(2ndmodel);
then this method of getting a list of indexes is unuseful ...
Can anyone help me to figure it out ?
I think I have to map them somehow to source indexes but dont know how :(

wysota
4th December 2007, 19:58
Selection is a property of the view, not of the model. If you set a proxy model on the view, you can use its selection model to get selected indexes of the proxy model which you can then map to the source model using its mapToSource() method.

gyre
4th December 2007, 20:04
Selection is a property of the view, not of the model. If you set a proxy model on the view, you can use its selection model to get selected indexes of the proxy model which you can then map to the source model using its mapToSource() method.

Yes but I was actually asking if there is a method to which I would actually passed the whole index list got byt selectionModel()->selectedRows() instead of iterating over all indexes....

jpn
4th December 2007, 20:07
Take a closer look at QAbstractProxyModel docs. ;)

gyre
4th December 2007, 20:22
Take a closer look at QAbstractProxyModel docs. ;)

man I made it work....but wanted something simpler...it wasnt hard of course...


foreach(QModelIndex index, msgTableView->selectionModel()->selectedRows()){
QModelIndex sourceIndex = filterModel->mapToSource(index);
int row = sourceIndex.row();
for(int i = 0; i < model->columnCount(); i++){
QModelIndex index2 = model->index(row, i);
QVariant prepMsg = model->data(index2);
if(i == 0){
sendMsg.id = prepMsg.toString().toULong();
qDebug("Message id = %ld", sendMsg.id);
}
if(i == 1){
sendMsg.flags = prepMsg.toString().toInt();
qDebug("Message flags = %d", sendMsg.flags);
}
if(i == 2){
QByteArray sendData = QByteArray::fromHex(prepMsg.toString().toAscii());
int length = qMin(sendData.size(), CAN_MSG_LENGTH);
qDebug("LENGTH = %d", length);
memcpy(sendMsg.data, (const unsigned char*)sendData.constData(), length);
sendMsg.length = length;
}
}
parentWindow->getNetworkIfc()->sendCanMsg(&sendMsg);
QVcaCanMsg sharedMsg(&sendMsg);
emit msgHistoryEnqueue(sharedMsg);
}

jpn
4th December 2007, 20:43
What's wrong with QAbstractProxyModel::mapSelectionToSource()?

gyre
4th December 2007, 21:57
What's wrong with QAbstractProxyModel::mapSelectionToSource()?

damn it...yes thats better solution...I remade it your way...But BOTH solutions work ;)

wysota
4th December 2007, 22:05
Doesn't it do exactly that?

gyre
4th December 2007, 22:11
Doesn't it do exactly that?

damn it...I was talking about something different ;)

wysota
4th December 2007, 22:31
Please don't modify your previous posts like that. It breaks the flow of the discussion and others can't benefit from it.