QSortFilterProxyModel - How to add an extra column.



  • Hello All,

    I'm trying to create a QSortFilterProxyModel subclass which has as its source a QSqlTableModel and provides two extra cols to supply extra data to view that is not in the source model.

    Where I'm having trouble is with the reimplemented index() method.

    From what I understand, if the column parameter is one of the 'extra' columns, I need to return an index using createIndex().
    What does one supply as the third parameter to createIndex() ?

    Thanks,
    Mike



  • I think I've figured it out.

    Since the source model in my case is a table ( as opposed to a tree ) model, reimplementing the parent() method to simply return an invalid index allows me to call createIndex() with simply the row and column.

    @
    QModelIndex CustomSortFilterProxyModel::parent(const QModelIndex &child) const
    {
    return QModelIndex();
    }

    QModelIndex CustomSortFilterProxyModel::index(int row, int column, const QModelIndex &parent) const
    {
    if ( row < rowCount() ) {
    switch ( column ) {
    case extraCol1:
    case extraCol2:
    return createIndex(row, column);
    }
    }
    return QSortFilterProxyModel::index(row, column, parent);
    }
    @

    Of course to fully support the additional columns, the following methods were also reimplemented:

    • columnCount()
    • data()
    • flags()
    • headerData()
    • mapToSource()
    • mapFromSource()

    Mike


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.