无法锚定到不是父级或同级 QML QtQuick 的项目

Swi*_*ngG 3 qt qml qtquick2

我正在使用 QML 开发 python 桌面应用程序。

我的 QML 文件中有这个:

SplitView {
    anchors.fill: parent
    orientation: Qt.Horizontal
    Rectangle {
        color: "#272822"
        id: cameraRectangle
        width: window.width / 2
        Item {
           //more stuff
        }
        Item {
            Rectangle {
                anchors.top: cameraRectangle.bottom
            }
        }
    }
    Rectangle {
      //Rectangle info.
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到错误“QML 矩形:无法锚定到不是父项或同级项的项目。” 在我正在做anchors.top的线上:cameraRectangle.bottom。我会假设外部矩形是内部矩形的父级?

我在网上搜索过,如下所示: http: //doc.qt.io/qt-5/qtquick-visualcanvas-visualparent.html,他们似乎没有做任何不同的事情?

难道是我使用的QtQuick版本的问题?

进口情况如下:

import QtQuick 2.6
import QtQuick.Controls 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Material 2.0
import QtQuick.Window 2.0
Run Code Online (Sandbox Code Playgroud)

我感谢您的帮助。

der*_*erM 5

SplitView {
    anchors.fill: parent
    orientation: Qt.Horizontal
    Rectangle {
        color: "#272822"
        id: cameraRectangle
        width: window.width / 2
        Item {
           //more stuff
        }
        Item {
            // The parent of this Item is 'cameraRectangle'
            // This Item will be the parent of the Rectangle
            // therefore the Rectangle can't anchor to the 'cameraRectangle'
            // anymore. As you are not doing anything with this Item
            // (so far?) anway, you can just delete it, and everything
            // will be fine.
            Rectangle {
                // The parent of this Rectangle is the Item that wraps it
                // and not the 'cameraRectangle'.
                anchors.top: cameraRectangle.bottom
            }
        }
    }
    Rectangle {
      //Rectangle info.
    }
}
Run Code Online (Sandbox Code Playgroud)

正如错误消息所述:除了父母之外,您无法锚定“祖先”。您也可以锚定兄弟姐妹。但既不是他们的孩子,也不是你的孩子,也不是你的任何“祖父母”、叔叔或阿姨;-)