【问题标题】:ListView in subwindow triggers immediate close, or whilst scrolling子窗口中的 ListView 触发立即关闭或滚动时
【发布时间】:2014-12-18 22:34:06
【问题描述】:

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

降低委托的复杂性将允许窗口打开,但随后快速滚动ListView 将强制关闭它。

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

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";
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

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

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

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

【问题讨论】:

  • win.createObject( null ); 创建一个没有父窗口的窗口,因此 QML 引擎会在触发资源释放时删除/关闭此窗口。试试Window { id: root; Component.onCompleted: { win.createObject( root ); } /*...*/ }。
  • @mcchu 这不是一个解决方案,因为创建的窗口的生命周期现在取决于父窗口的生命周期 - 这从根本上改变了应用程序的行为。
  • @cmannett85 我不完全确定你 4 年前在这里想要完成什么。但是现在阅读它,我认为您可以使用 StackView 或 TabView 甚至 SwipeView 来代替手动管理多个 Window 对象。根据您的用例,如果您在 Android 上需要两个“Windows”,甚至有两个项目/矩形/窗格,您的内容填充根窗口并使一个或另一个可见,或者(卸载)一个或另一个使用 Loader 可能是一个解决方案。甚至简单地使用 Dialog 也可能是您想要的,这些在 Android 上运行良好。

标签: qt debugging qml qt-quick


【解决方案1】:

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

真正的原因是因为你原来是来自 QML 的cannot create new root contexts(即顶级窗口)。这在 Qt Quick Components 发布时暗示已解决,但 blog post 吹嘘 Window 并没有明确说明这一点。创建一个新的Window 并为父级传递null技术上 可行,但结果似乎非常不稳定。

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

【讨论】:

  • 我仍然删除了答案,因为重点是研究 那个 特殊情况。我很抱歉没有直截了当。确实是这样,并且通过对原始代码进行一些修改来确认:如果您在父 Window 中设置 id 并将其传递给 createObject,则代码可以完美运行(因此使用外部 @987654331 的上下文@)。 IMO 对createObject 感兴趣this part of the documentation。
猜你喜欢
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
  • 2020-11-08
相关资源
最近更新 更多