如何在添加项目时将项目视图滚动到底部? [英] How to keep an item view scrolled to the bottom when items are added?

查看:273
本文介绍了如何在添加项目时将项目视图滚动到底部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在 QAbstractItemView 所示的模型末尾追加项目时,我希望将视图保留在数据底部,显示最近添加的项目。默认行为是保留最近显示的项目的位置,但是不滚动,如果我们在底部。

When appending items at the end of a model shown by a QAbstractItemView, I wish to keep the view at the bottom of the data, showing the most recent added items. The default behavior is to retain the most recently displayed item's position, but not to scroll if we were at the bottom.

保持视图所需的魔力是什么

What would be the magic needed to keep the view at the bottom if the user has previously scrolled it all the way to the bottom?

推荐答案

QListView view;
bool viewAtBottom = false;

在添加项目之前,请检查视图是否一直滚动到底部。 p>

Before an item is added, check if the view is scrolled all the way to the bottom.

connect(view.model(), &QAbstractItemModel::rowsAboutToBeInserted,
        &view, [&] {
  auto bar = view.verticalScrollBar();
  viewAtBottom = bar ? (bar->value() == bar->maximum()) : false;
});

插入项目后,如果视图之前位于项目之前,滚动到底部

After an item is inserted, scroll to the bottom if the view was previously at the bottom before the item got added.

connect(view.model(), &QAbstractItemModel::rowsInserted,
        &view, [&]{ if (viewAtBottom) view.scrollToBottom(); });

这篇关于如何在添加项目时将项目视图滚动到底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆