0
votes

I have a working Qtableview with custom model subclassed QAbstractTableModel and QAbstractItemModel.

I have a Qlineedit, onclicked it will filter the view:

// model.cpp setFilter(QString strFilter) function searches trough my intern QList (this Qlist is actually attached to model) and if match found then: m_filterSet.insert(i);

This all works great. Problem is, i have CRUD operations for the tableview (insert row, delete row..) which also work great! But when selecting a row from a filtered set, i need to somehow know where in my QList exactly is this selected row from the filtered set (QSet ).

ui.myView->selectionModel()->currentIndex().row();

obvious gives the wrong indexes counting for the current view.

How can i somehow extract the value (int) from the selected row in the QSet? Because when i added this function to model:

foreach (const int &value, m_filterSet)
        qDebug() << value;

It has printed out successfully all the i values, e.g: 3410, 3411, 3412 (those are my client id's)

If i could extract this ID for the selected row in Qset, i could write a function that iterates my intern QList, and find a matching, so to speak:

if(m_Intern[i].nClientID == nId){   // nId = value inside Qset for selected row in view
    return nIdx;
}
2

2 Answers

4
votes

Qt has a solution for your problem - just use QSortFilterProxyModel. You will need to:

  • Subclass it and write your own filtering function (filterAccpetsRow)
  • Proxy your original model through filtering one
  • Attach filtering model to a view
  • use QSortFilterProxyModel::mapToSource() to convert between indexes in filtered and original model.

This allows you to have more than one view with just one source data model, each view may have different filters.

0
votes

I solved it after a while re thinking, i just needed to implement another function inside my model:

int myClass::screenIndex2DataIndex(int nIdxScreen)
{
   if(m_bUseFilter)
    {
        int nIdx =-1;
        for(int i=0;i<m_lstIntern.size();i++)
        {
            if(m_filterSet.contains(i))
            {
                nIdx++;
                if(nIdx == nIdxScreen){
                    return i;   
                }
            }
        }
        return -1; //not found
    }
    else{
        return nIdxScreen;
    }
}

This way i can find out for the present index on the filtered view, where it is in my intern list.

After this it's easy to get my nClientID trough a return: return m_lstIntern[idx].nClientId