Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to show only certain indexes of a QML listview?

Tags:

listview

qt

qml

Is it possible to show only certain indexes or a range of indexes in QML listviews?

I have a listmodel that has a bunch of info that I am reusing. Would it be possible to have a list showing, for example, only indices 5 to 8?

like image 302
CantThinkOfAnything Avatar asked Mar 09 '15 11:03

CantThinkOfAnything


2 Answers

You could accomplish that by setting the delegate visibility to false on certain conditions:

  Grid {
    anchors.fill: parent
    rows: 4
    columns: 5
    spacing: 5
    Repeater {
      model: 20
      delegate: Rectangle {
        width: 50
        height: 50
        visible: index >= 5 && index <= 8 // show only certain indices
        Text {
          anchors.centerIn: parent
          text: index
        }
      }
    }
  }

enter image description here

It will have some overhead of creating hidden items in memory thou, so not optimal if you deal with very large models and showing only small portions of them.

like image 97
dtech Avatar answered Oct 23 '22 21:10

dtech


Yes, it is possible. You need to override QSortFilterProxyModel::filterAcceptRow method.

MyFilterModel::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
{
  if ( source_row > 5 && source_row < 8 )
    return true;

  return false;
}
//...
MyFilterModel *filter = new MyFilterModel();
filter->setSourceMoldel( yourSourceModel );
view->setModel( filter );
like image 43
Dmitry Sazonov Avatar answered Oct 23 '22 20:10

Dmitry Sazonov