Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

[SOLVED] Strange interaction of QAbstractItemModel with QSortFilterProxyModel

  • What is wrong with this an interaction of QAbstractItemModel with QSortFilterProxyModel? On left part of "screen": I connected my implementation of QAbstractItemModel and on right part part I did QSortFilterProxyModel.

    Upd: git repo:

    @ListsRegisterModel *model = new ListsRegisterModel(this);

    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);

    QTreeView *view = new QTreeView(this);

    QTreeView *view2 = new QTreeView(this);

    @class ListsRegisterModel : public QAbstractItemModel
    explicit ListsRegisterModel(QObject *parent = 0);

    QVariant data(const QModelIndex &index, int role) const;
    Qt::ItemFlags flags(const QModelIndex &index) const;
    QVariant headerData(int section, Qt::Orientation orientation,
                        int role = Qt::DisplayRole) const;
    QModelIndex index(int row, int column,
                      const QModelIndex &parent = QModelIndex()) const;
    QModelIndex parent(const QModelIndex &index) const;
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    bool setData(const QModelIndex &index, const QVariant &value,
                 int role = Qt::CheckStateRole);

    enum Columns
    ListNameColumn = RamificationColumn,

    TreeItem *rootItem;
    QMultiMap<QString, QString> parseListsFile&#40;QString path&#41;;
    void setupModelData(QMultiMap<QString, QString> lists, TreeItem *parent&#41;;
    TreeItem *getItem(const QModelIndex &index&#41; const;


    ListsRegisterModel::ListsRegisterModel(QObject *parent) :
    QList<QVariant> rootData;
    rootData << tr("List") << tr("Subscribers");
    rootItem = new TreeItem(rootData);

    QMultiMap<QString, QString> listsReg = parseListsFile&#40;"etc/lists.reg"&#41;;
    setupModelData(listsReg, rootItem&#41;;


    QVariant ListsRegisterModel::data(const QModelIndex &index, int role) const
    if (!index.isValid())
    return QVariant();

    TreeItem *item = getItem(index);
    switch (role) {
    case Qt::DisplayRole:
        if (item->parent() == rootItem) {
            return item->data(index.column());
        else {
            qDebug() << item->data(index.column() - 1);
            return item->data(index.column() - 1);
    case Qt::CheckStateRole:
        if (index.column() == RamificationColumn
                && item->parent() == rootItem) {
            return QVariant(item->checkState());
        return QVariant();
    return QVariant();


    QModelIndex ListsRegisterModel::index(int row, int column, const QModelIndex &parent) const
    TreeItem * parentItem;

    if (row < 0 || column < 0)
        return QModelIndex();
    if (!parent.isValid())
        parentItem = rootItem;
        parentItem = getItem(parent);
    TreeItem *childItem = parentItem->child(row);
    if (childItem)
        return createIndex(row, column, childItem);
    return QModelIndex();


    QModelIndex ListsRegisterModel::parent(const QModelIndex &index) const
    if (!index.isValid())
    return QModelIndex();

    TreeItem *childItem = getItem(index);
    TreeItem *parentItem = childItem->parent();
    if (parentItem == rootItem)
        return QModelIndex();
    return createIndex(parentItem->row(), 0, parentItem);


  • I'm sprinkling ashes upon my head. I defined falsely columnCount() function. The correct version will be this:

    @int ListsRegisterModel::columnCount(const QModelIndex &parent) const
    return LastColumn; // = 2

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    great you found out and shared !

    Please update your thread title prepending [solved] so other forum users may know a solution has been found :)

Log in to reply