Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to limit shown rows in list view?
Forum Update on Monday, May 27th 2025

How to limit shown rows in list view?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 372 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Subuday
    wrote on 17 May 2019, 15:54 last edited by
    #1

    I can't figure out how to limit shown items in list view using filter model?

        filterModel->setSourceModel(&langListModel);
        filterModel->setFilterRole(langListModel.NameRole);
    ``
    
    

    #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();
    }

    J 1 Reply Last reply 17 May 2019, 18:18
    0
    • S Subuday
      17 May 2019, 15:54

      I can't figure out how to limit shown items in list view using filter model?

          filterModel->setSourceModel(&langListModel);
          filterModel->setFilterRole(langListModel.NameRole);
      ``
      
      

      #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();
      }

      J Offline
      J Offline
      JonB
      wrote on 17 May 2019, 18:18 last edited by
      #2

      @Subuday

      if (parent.isValid()) {
       return 0;
       }
      

      You sure that's not supposed to be if (!parent.isValid())?

      S 1 Reply Last reply 17 May 2019, 18:19
      1
      • J JonB
        17 May 2019, 18:18

        @Subuday

        if (parent.isValid()) {
         return 0;
         }
        

        You sure that's not supposed to be if (!parent.isValid())?

        S Offline
        S Offline
        Subuday
        wrote on 17 May 2019, 18:19 last edited by
        #3

        @JonB Thanks!

        1 Reply Last reply
        0

        1/3

        17 May 2019, 15:54

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved