How show only part of elemensts of list view?



  • I have list model and I need to show first 10 elements. Also I use qml.

    #include "langlistmodel.h"
    
    LangListModel::LangListModel(QObject *parent):
        QAbstractListModel(parent)
    {
    
    }
    
    
    int LangListModel::rowCount(const QModelIndex &parent) const
    {
        if (parent.isValid()) {
            return 0;
        }
    
        return listData.size();
    }
    
    QVariant LangListModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid()) {
            return QVariant();
        }
    
        switch (role) {
        case IDRole:
            return listData.at(index.row()).value("id");
        case NameRole:
            return listData.at(index.row()).value("name");
        default:
            return QVariant();
        }
    }
    
    QHash<int, QByteArray> LangListModel::roleNames() const
    {
        QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
        roles[NameRole] = "name";
        roles[IDRole] = "IDLang";
    
        return roles;
    }
    
    void LangListModel::add(const QMap<QString, QString> &unit)
    {
        beginInsertRows(QModelIndex(), listData.size(), listData.size());
        listData.append(unit);
        endInsertRows();
    }
    
    int LangListModel::size() const
    {
        return listData.size();
    }
    
    void LangListModel::clear()
    {
        beginRemoveRows(QModelIndex(), 0, listData.size());
        endRemoveRows();
        listData.clear();
    }
    
      ListView {
         id: mentorsList
         model: filterModel
         footer: footerAndHeader
         anchors.fill: parent
         spacing: 2
         cacheBuffer: 2
         clip: true
    

    ......// Some Code
    }


  • Lifetime Qt Champion

    Hi,

    Use a proxy model and implement the filterAcceptsRow to accept only the 10 first object.


Log in to reply