Cannot anchor to an item that isn't a parent or sibling QML QtQuick

↘锁芯ラ 提交于 2019-12-11 05:27:01

问题


I'm working on a python desktop app using QML.

I have this in my QML file:

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.
    }
}

I get the error that "QML Rectangle: Cannot anchor to an item that isn't a parent or sibling." On the line where I am doing anchors.top: cameraRectangle.bottom. I would have assumed that the outer rectangle IS a parent of the inner one?

I have searched online like here: http://doc.qt.io/qt-5/qtquick-visualcanvas-visualparent.html and they don't seem to be doing anything differently?

Could it be the version of QtQuick I am using?

The imports are as follows:

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

I appreciate your help.


回答1:


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.
    }
}

As the error message stated: you can't anchor to 'ancestors' other than your parent. You can also anchor to siblings. But neither to their children, nor to yours, and not to any of your 'grand-parents', uncles or aunts ;-)



来源:https://stackoverflow.com/questions/40128852/cannot-anchor-to-an-item-that-isnt-a-parent-or-sibling-qml-qtquick

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!