1、错误一:qrc:/main.qml:30:5: QML Frame: Cannot anchor to an item that isn't a parent or sibling
.
QML的anchor必须定位父级对象或者同级对象,不能定位到其他如:同级对象的子对象。
//main.qml
import QtQuick 2.0
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 1.4ApplicationWindow {id: windowvisible: truewidth: 500height: 320Frame {id: f0Frame {id: f1width: 100height: 100background: Rectangle {color: "red"}// 必须首先设置锚点,然后才能设置margin,否则不生效anchors.left: parent.leftanchors.top: parent.topanchors.leftMargin: 10anchors.topMargin: 10}}Frame {id: f2width: 100height: 100background: Rectangle {color: "green"}anchors.top: f1.bottomanchors.left: f1.rightanchors.leftMargin: 10}
}
运行上述错误,原因就是在f2中锚定位了与其同级对象f0的子对象f1了。