当前位置: 代码迷 >> 综合 >> 【QML】ScrollView与其子空间间有两层parent
  详细解决方案

【QML】ScrollView与其子空间间有两层parent

热度:52   发布时间:2023-10-21 01:48:27.0

一般的QML控件访问其父控件只需要一个parent,但在某一次使用 ScrollView 的时候出了问题。

我们来看看 ScrollView 下面的子控件是什么情况:

    ScrollView {id: scrollView1Rectangle{id:rectangle_test}}

我们逐步去访问 rectangle 的父控件:

            console.log(rectangle_test)console.log(rectangle_test.parent)console.log(rectangle_test.parent.parent)console.log(rectangle_test.parent.parent.parent)console.log(rectangle_test.parent.parent.parent.parent)console.log(rectangle_test.parent.parent.parent.parent.parent)

得到:

qml: QQuickRectangle(0x482c5190)
qml: QQuickItem(0x48291b50)
qml: QQuickFlickable(0x48291a60)
qml: QQuickScrollView(0x482c5790)
qml: null
qrc:/EXC_Control_ButtonBox.qml:230: TypeError: Cannot read property 'parent' of null

咦,中间多了两层,一层item,一层flickable!

  相关解决方案