How can i get hold of all of the visible items in my QListView?

How can i get hold of all of the visible items in my QListView?
In order to get hold of the visible items in a QListView http://doc.qt.io/qt-5/latest/qlistview.html, then you can iterate over them using indexAt() http://doc.qt.io/qt-5/latest/qlistview.html#indexAt. You can get hold of the first visible item using indexAt(QPoint(0, 0)), then in order to get the index at the next position then use visualRect() http://doc.qt.io/qt-5/latest/qlistview.html#visualRect to find out what your next call to itemAt() should be. This position would be:


visualRect.y() + visualRect.height() + 1 effectively.
See the following example for an illustration:


#include <QtGui>
QList <QModelIndex>myList;
 
class ListView : public QListView
{
  Q_OBJECT
public:
  ListView()
  {
    QStringListModel *myModel = new QStringListModel(this);
    QStringList list;
    list << "a" << "b" <<"c" <<"d" <<"e" <<"f" <<"g" <<"h" <<"i" <<"j" <<"k";
    myModel->setStringList(list);
    setModel(myModel);
    QTimer::singleShot(3000, this, SLOT(test1()));
  }
  public slots:
    void test1()
    {
 
      QModelIndex firstIndex = indexAt(QPoint(0, 0));
      if (firstIndex.isValid()) {
        myList << firstIndex;
      } while (viewport()->rect().contains(QPoint(0, visualRect(firstIndex).y() + visualRect(firstIndex).height() + 1 ))) {
        firstIndex = indexAt(QPoint(0, visualRect(firstIndex).y() + visualRect(firstIndex).height() + 1 ));
        myList << firstIndex;
      }
      qDebug() << myList.count() << "are visible";
    }
};
 
#include "main.moc"
 
int main(int argc, char** argv)
{
  QApplication app(argc, argv);
  ListView window;
  window.resize(100, 50);
  window.show();
  return app.exec();
 
}


转载:https://wiki.qt.io/Qt_project_org_faq#How_can_i_get_hold_of_all_of_the_visible_items_in_my_QListView.3F
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值