0

I have a simple logic, that basically goes like this: a few entries are filtered from source model (my custom QAbstractTableModel) and presented to the user using QSortFilterProxyModel, which does have only modified filterAcceptsRow function. This presentation is done using simple dialog. User selects desired entries from filtered ones and those selected entries from model must be updated (actually two fields have to be modified). So simplified code goes like this:

    QModelIndexList selectedRows = myProxyModel->selectionModel()->selectedRows();
    for (int i = 0; i < selectedRows.count(); i++) {
        myProxyModel->setData(myProxyModel->index(selectedRows.at(i).row(), (int) LoanStatusCol, QModelIndex()), (int) ReturnedLoan, Qt::EditRole);
        myProxyModel->setData(myProxyModel->index(selectedRows.at(i).row(), (int) LoanRetEntriesCol, QModelIndex()), (lastEntryNo + 1), Qt::EditRole);
    }

However, this does not work. And each time behavour is quite weird. What I noticed is that, when it gets to second selected row in this cycle and when it reaches setData() code in the model:

bool TransactionModel::setData(const QModelIndex &index, const QVariant &value, int role) {
    if (!index.isValid()) {
        return false;
    }

it returns invalid index. However, when I swaped these two setData() code lines, one row was updated, but second row was not - due to invalid index. I do not know, whether I explained that correctly, but probably this should be my silly mistake, because I am new at this.

UPDATE: Since model consists of QList data, where Transaction is a custom class that defines fields of entry, I created a function, that updated underlying entry by column number (so to say...). I use function setValueByColumnNo. I just could not find a better way to do that, when working with lists of custom classes.

bool TransactionModel::setData(const QModelIndex &index, const QVariant &value, int role) {
    if (!index.isValid()) {
        return false;
    }
    if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
        transactionData[index.row()].setValueByColumnNo(index.column(), value);  
        emit dataChanged(index, index);
        return true;
    }
    return false;
}

Any ideas?

Thanks.

rofl
  • 343
  • 3
  • 18
  • Try to change `Qt::EditRole` to `Qt::DisplayRole`. Edit role is not related to displaying data, so your view will not update. – Dmitry Sazonov Oct 29 '15 at 15:54
  • Could you please provide complete (copy & paste) TransactionModel::setData code – samdavydov Oct 29 '15 at 16:05
  • Changing to DisplayRole did not work. I made an update. – rofl Oct 29 '15 at 16:17
  • are you absolutely certain that `LoanStatusCol` and `LoanRetEntriesCol` are valid columns in your model? There are not too many ways to create invalid indexes, especially using `QAbstractItemModel::index()`. I'd say, tracing back the reason for the invalid index will solve your problem. – Basti Vagabond Oct 29 '15 at 19:58
  • Well, they are simple integers (enumerators). What is weird, that, when I try to modify the table manually (editing fields) everything works fine. Moreover, as I mentioned, with a single selected line, it works OK. It even works, when I swap those two lines of code: first one gets updated and second one - does not. – rofl Oct 29 '15 at 20:02
  • Well, it looks like I know the problem. My proxymodel displays filtered entries. Dialog window shows that filtered list. However, those two setData() lines actually modify exactly the same fields that I used to filter. Therefore, when modifying the data selected indexes were screwed... I tried to change those selected QModelIndex variables to QPersistentModelIndex variables and then it worked fine - problem seems to be solved. But is it the only way, how to modify data in such situation (i.e. when updating fields that were exactly used to filter current filtered list [and not to loose index]? – rofl Oct 29 '15 at 20:19

0 Answers0