qt - 更改模型时如何停止 "jumping"的 ListView

标签 qt listview model qml qtquick2

我需要做什么 : 我需要创建一个 聊天窗口使用 ListView在存储聊天消息的 QML 中。我设置了listView.positionViewAtEnd()以便跟踪最后的消息。我禁用 positionViewAtEnd当我向上滚动以便每次收到新消息时我都可以阅读过去的消息而不会跳到最后。

问题 : 向上滚动后,每次我收到一条新消息,它跳转 在列表的开头。为了解决这个问题,我设法 存储 contentY列表并每次重置它 onCountChanged处理程序被称为 (见下面的代码):

ListView {
    id: messagesList
    model: contact? contact.messages: []
    delegate: delegate
    anchors.fill: parent
    anchors.bottomMargin: 20
    height: parent.height
    anchors.margins: 10

    property int currentContentY

    onMovementEnded: {
       currentContentY = contentY
    }

    onCountChanged: {
        contentY = currentContentY
    }
    onContentYChanged: {
        console.log(".....contentY: " + contentY)
    }
}   

问题是即使我设置了最后一个 contentY我有,在模型改变之前,列表仍然有点跳跃 ( 几个像素 ,不在结尾或开头)和 它并不总是跳跃 .当我转到列表顶部并打印 contentY我得到负值 .理论上,contentY列表的开头应该是 0 .

有人能告诉我出了什么问题吗?或者也许建议另一种解决方案来创建我的消息列表?

比你提前! :)

最佳答案

一种可能的解决方案应该是将 ListView 插入 Flickable 并禁用 互动 ListView 的标志

    Flickable {
        id: fparent
        anchors.fill: parent
        anchors.bottomMargin: 20
        anchors.margins: 10

        interactive: true
        clip: true
        flickableDirection: Flickable.VerticalFlick
        contentHeight: messagesList.height

        ListView {
            id: messagesList
            width: parent.width
            height: childrenRect.height
            clip: true
            model: contact? contact.messages: []
            delegate: delegate
            interactive: false
            onCountChanged: {
                fparents.returnToBounds();
            }
        }
    }

关于qt - 更改模型时如何停止 "jumping"的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043778/

相关文章:

android - 如何在 Android 中创建自定义 ListView?

ruby-on-rails - 这种计算是放​​在Model还是Controller?

php - 在 Laravel Eloquent 模型中使用特殊的 union 函数

c++ - Qt信号槽重复代码避免

c++ - 如何将Qt文件路径从资源路径转换为绝对路径?

listview - Flutter 仅按需填充 ExpansionTile 数据(一个 Future 对象),而不是在初始化时

android - 按钮监听器和 SimpleCursor 适配器

c++ - QObject::connect: 无法连接 (null)::mySignal() 到 (null)::mySlot()

debugging - 在单步执行期间观看 QImage

php - 保存模型时的 Laravel 模型尾随数据