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

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

问题描述

我有一个非常奇怪的情况,如果我启动一个子窗口,其中包含一个 ListView 与中等复杂的委托和足够的项目舒适地超过可见区域,整个子窗口将立即关闭发布。



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



SSCCE 会触发对我的笔记本电脑的影响,但是在一个更强大的机器上,它可能只是在滚动(或者代表可能需要更复杂)时执行它:

 code> import QtQuick 2.3 
import QtQuick.Window 2.0

窗口{
width:300
height:200

组件.onCompleted:{
win.createObject(null);
}

组件{
id:win

窗口{
width:600
height:400

visible:true

ListView {
id:view
anchors.fill:parent

model:100

boundsBehavior:Flickable.StopAtBounds
剪辑:true

委托:Rectangle {
width:view.width
height:24

属性int debugLevel:index%3
属性int timestamp:index * 1000
属性int message:index

颜色:darkgray

Row {
anchors.fill:parent

中继器{
id:delegateRepeater

属性列表< QtObject> roleModel:[
QtObject {
属性字符串标签:timestamp
属性int itemWidth:100
},
QtObject {
属性字符串标签:debugLevel
属性int itemWidth:100
},
QtObject {
属性字符串标签:消息
属性int itemWidth:view.width - 100 - 100
}
]

model:roleModel

项目{
width:itemWidth
anchors {
顶部:parent.top
bottom:parent.bottom
}

文本{
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 )中添加一个断点,那么有一个单一的堆栈条目指向模型:roleModel 语句 - 但删除整个 Repeater 并替换为一个复制和粘贴的等效项产生相同的结果减去堆栈条目。



所以我很感激有人知道从这个纯QML示例中获取更多信息的方法。

解决方案

正如@BaCaRoZzo所指出的,通过修改代理代码改变行为似乎是一个无关的副作用。



真正的原因是因为事实证明您无法从QML创建新的根上下文(即顶级窗口)。这在Qt Quick Components被发布时被暗示解决,但是博客文章吹嘘窗口没有明确说明这一点。创建一个新的窗口,并且通过 c 非常不稳定。



感谢在我的情况下,我正在创建一个QML / C ++应用程序,所以我已经通过从 C ++方面的Q_INVOKABLE 方法。但是,如果您正在开发纯粹的QML应用程序,那么您似乎没有运气。


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.

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

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.

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.

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

解决方案

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

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.

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天全站免登陆