我试图在 main.qml 文件中使用 idcontainerItem在主窗口中定位具有 id 的项目mainWindow。但是我在输出中收到了这条消息:
无法将 QQuickWindowQmlImpl 分配给 QQuickItem
消息指向字符串anchors.fill: mainWindow。我的代码:
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
visible: true
color: "black"
id : mainWindow
width: 360
height: 360
Item {
anchors.fill: mainWindow
id: containerItem
}
}
Run Code Online (Sandbox Code Playgroud)
我将非常感谢您的帮助!
小智 5
Window( QQuickWindow) 非常具体。如果您将 child 称为 parent ( anchors.fill:parent),则会收到QQuickRootItem,但是如果您使用id,就像在您的示例中一样,则会收到QQuickWindowQmlImpl. 如果您想让anchors.fill孩子使用,请使用Windowas parent。PS 也Window根本没有锚点,如果你想在例子中粘住,你必须计算你想粘住的窗口和对象的宽度,或者你可以将对象包裹到填充的 Item 中。
例子:
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
id : mainWindow
visible: true
color: "black"
width: 360
height: 360
Item {
id: containerItem
anchors.fill: parent
Rectangle {
anchors {
right:parent.right
bottom:parent.bottom
}
width: 80
height: 80
color: "pink"
}
}
}
Run Code Online (Sandbox Code Playgroud)