子窗口中的 ListView 触发立即关闭或滚动时 [英] ListView in subwindow triggers immediate close, or whilst scrolling

查看:10
本文介绍了子窗口中的 ListView 触发立即关闭或滚动时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当奇怪的场景,如果我启动一个包含 ListView 的子窗口,该子窗口具有中等复杂的委托和足够的项目以轻松超出可见区域,则整个子窗口将在启动时立即关闭.

I have rather strange scenario whereby if I launch a subwindow that contains a ListView with a moderately complex delegate and enough items to comfortably exceed the visible area, the entire subwindow will immediately close on launch.

降低delegate的复杂度将允许窗口打开,但随后快速滚动ListView将强制关闭它.

Reducing the complexity of the delegate will allow the window to open, but then rapidly scrolling the ListView will forcibly close it.

这个 SSCCE 会在我的笔记本电脑上触发效果,但在功能更强大的机器上,它可能只会在滚动时触发(或者委托可能需要更复杂):

This SSCCE triggers the effect on my laptop, but on a more powerful machine it may only do it whilst scrolling (or perhaps the delegate may need to be more complex):

import QtQuick 2.3
import QtQuick.Window 2.0

Window {
    width: 300
    height: 200

    Component.onCompleted: {
        win.createObject( null );
    }

    Component {
        id: win

        Window {
            width: 600
            height: 400

            visible: true

            ListView {
                id: view
                anchors.fill: parent

                model: 100

                boundsBehavior: Flickable.StopAtBounds
                clip: true

                delegate: Rectangle {
                    width: view.width
                    height: 24

                    property int debugLevel: index % 3
                    property int timestamp: index * 1000
                    property int message: index

                    color: "darkgray"

                    Row {
                        anchors.fill: parent

                        Repeater {
                            id: delegateRepeater

                            property list< QtObject > roleModel: [
                                QtObject {
                                    property string label: timestamp
                                    property int itemWidth: 100
                                },
                                QtObject {
                                    property string label: debugLevel
                                    property int itemWidth: 100
                                },
                                QtObject {
                                    property string label: message
                                    property int itemWidth: view.width - 100 - 100
                                }
                            ]

                            model: roleModel

                            Item {
                                width: itemWidth
                                anchors {
                                    top: parent.top
                                    bottom: parent.bottom
                                }

                                Text {
                                    anchors {
                                        fill: parent
                                        leftMargin: 4
                                    }

                                    verticalAlignment: Text.AlignVCenter

                                    text: label
                                    elide: Text.ElideRight
                                }

                                Rectangle {
                                    anchors {
                                        top: parent.top
                                        bottom: parent.bottom
                                        right: parent.right
                                    }

                                    width: 1

                                    visible: index != ( delegateRepeater.count - 1 )
                                    color: "white";
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

似乎没有导致问题的代码的任何特定部分,删除委托中的任何对象会降低子窗口关闭的可能性.

There doesn't seem to be any particular part of the code that is causing the problem, removing any of the objects in the delegate reduces the probability of the subwindow closing.

我添加了调试标签,因为我的主要问题是这个效果不会产生调试输出.如果我在子窗口的销毁处理程序 (Component.onDestruction) 中添加断点,那么会有一个堆栈条目指向 model: roleModel 语句 - 但会删除整个 Repeater 并用复制粘贴的等效项替换会产生相同的结果减去堆栈条目.

I've added the debugging tag because my main problem is that this effect produces no debug output. If I add a breakpoint into the subwindow's destruction handler (Component.onDestruction) then there is a single stack entry pointing at the model: roleModel statement - but removing the entire Repeater and replacing with a copy-and-pasted equivalent yields the same results minus the stack entry.

如果有人知道从这个纯 QML 示例中获取更多信息的方法,我将不胜感激.

So I would be grateful is anyone knows of a way of getting more information from this pure QML example.

推荐答案

正如@BaCaRoZzo 所指出的,通过修改委托代码来改变行为似乎是一个不相关的附带问题.

As noted by @BaCaRoZzo the changing of behaviour by modifying the delegate code seems to be an unrelated side-issue.

真正的原因是因为事实证明你 无法创建新的根上下文(即顶级窗口)来自 QML.这在 Qt Quick Components 发布时暗示已解决,但 博客文章 吹嘘 Window 并没有明确说明这一点.创建一个新的 Window 并为父级传递 null 技术上 是可行的,但结果似乎非常不稳定.

The real cause is because it turns out you cannot create new root contexts (i.e. top-level windows) from QML. This was hinted at being resolved when Qt Quick Components were released, but the blog post boasting of Window doesn't explicitly state this. Creating a new Window and passing null for the parent technically works but the result seems to be very unstable.

幸运的是,在我的情况下,我正在创建一个 QML/C++ 应用程序,因此我通过在 C++ 端从 Q_INVOKABLE 方法创建新的根上下文来解决了这个问题.但是如果你正在开发一个纯粹的 QML 应用程序,那么你似乎很不走运.

Thankfully in my circumstance I'm creating a QML/C++ application so I've solved the issue by creating new root contexts from Q_INVOKABLE methods on the C++ side. But if you're developing a pure QML application, it seems that you are out of luck.

这篇关于子窗口中的 ListView 触发立即关闭或滚动时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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